From 9a47a6b11d6f07c95bbf5b94fe1dc91e2d4af9ae Mon Sep 17 00:00:00 2001 From: Tom Donaldson Date: Thu, 5 Mar 2026 09:12:19 -0500 Subject: [PATCH 01/17] Allow GraphicOverlay.increaseBrightness() to accept named colors --- src/js/Overlay.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/js/Overlay.js b/src/js/Overlay.js index 2ab51a12..72ae6460 100644 --- a/src/js/Overlay.js +++ b/src/js/Overlay.js @@ -309,12 +309,15 @@ export let GraphicOverlay = (function() { * * @memberof GraphicOverlay * - * @param {string} hex - The color given in hexadecimal e.g. '#ffa0bb' + * @param {string} color - Any CSS color string (hex e.g. '#ffa0bb', rgb(), named color) * @param {number} percent - The percentage to increase the brightness of * * @returns {string} The new color given as an hexadecimal string */ - GraphicOverlay.increaseBrightness = function(hex, percent){ + GraphicOverlay.increaseBrightness = function(color, percent){ + // Normalize the color to hex. + var hex = Color.standardizeColor(color); + // strip the leading # if it's there hex = hex.replace(/^\s*#|\s*$/g, ''); From 6dc1722cd27e40fd2adbc4341c395b6e3b9bf6ca Mon Sep 17 00:00:00 2001 From: Tom Donaldson Date: Thu, 5 Mar 2026 09:38:50 -0500 Subject: [PATCH 02/17] Update change log. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b44aadf8..b4faae3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### 3.8.0 + +* [fix] Fix shape hover colors when overlay uses named color (increaseBrightness() accepts names now) * [feat] Selected and hovered items (shapes, sources, ...) are rendered at last. * [feat] Add selectionLineWidth option for shapes and catalogs. * [fix] horizontal/vertical overlay lines appearing correctly From af3f844bccbc4ac113e481faf61e0e73c966ce2a Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Fri, 6 Mar 2026 16:41:38 +0100 Subject: [PATCH 03/17] fix incl of aladin lite script in head --- examples/al-badbaselayer.html | 6 +- examples/al-multiple-instances.html | 4 +- examples/index.html | 6 +- src/core/src/shaders.rs | 8053 +++++++++++++++++++++++- src/js/A.js | 5 +- src/js/Aladin.js | 6 +- src/js/DefaultActionsForContextMenu.js | 20 +- src/js/ProgressiveCat.js | 2 +- src/js/View.js | 14 +- src/js/gui/Box/StackBox.js | 2 +- src/js/gui/Input/HiPSSelector.js | 2 +- src/js/gui/Widgets/Box.js | 4 +- 12 files changed, 7899 insertions(+), 225 deletions(-) diff --git a/examples/al-badbaselayer.html b/examples/al-badbaselayer.html index d2aed520..21143eb5 100644 --- a/examples/al-badbaselayer.html +++ b/examples/al-badbaselayer.html @@ -7,10 +7,10 @@ diff --git a/examples/al-multiple-instances.html b/examples/al-multiple-instances.html index 7b2f4f95..eae86d44 100644 --- a/examples/al-multiple-instances.html +++ b/examples/al-multiple-instances.html @@ -3,9 +3,9 @@ -
+
-
+
+ + +
- diff --git a/examples/al-zoomchanged.html b/examples/al-zoomchanged.html index 7695bf1b..decd2414 100644 --- a/examples/al-zoomchanged.html +++ b/examples/al-zoomchanged.html @@ -21,7 +21,7 @@ console.log("Rotation just changed to ", rotation); }) aladin.on("positionChanged", ({ra, dec}) => { - console.log('call to aladin', aladin.pix2world(300, 300)) + console.log('call to aladin', aladin.pix2world(300, 300, "galactic")) console.log('positionChanged in icrs', ra, dec) }) diff --git a/package.json b/package.json index c548922e..4174df5a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "homepage": "https://aladin.cds.unistra.fr/", "name": "aladin-lite", "type": "module", - "version": "3.8.2", + "version": "3.9.0-beta", "description": "An astronomical HiPS visualizer in the browser", "author": "Thomas Boch and Matthieu Baumann", "license": "GPL-3", diff --git a/src/core/Cargo.toml b/src/core/Cargo.toml index 70a3eb1d..81184d93 100644 --- a/src/core/Cargo.toml +++ b/src/core/Cargo.toml @@ -25,7 +25,8 @@ wasm-bindgen = "=0.2.92" async-channel = "1.8.0" mapproj = "0.3.0" colorgrad = "0.6.2" -fitsrs = "0.4.1" +#fitsrs = "0.4.1" +fitsrs = { git = "https://github.com/cds-astro/fitsrs", branch = "master" } [features] webgl1 = [ "al-core/webgl1", "al-api/webgl1", "web-sys/WebGlRenderingContext", "web-sys/AngleInstancedArrays", "web-sys/ExtSRgb", "web-sys/OesTextureFloat",] diff --git a/src/core/al-api/src/coo_system.rs b/src/core/al-api/src/coo_system.rs index eb623980..b3f267aa 100644 --- a/src/core/al-api/src/coo_system.rs +++ b/src/core/al-api/src/coo_system.rs @@ -31,8 +31,8 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] #[derive(Clone, Copy, PartialEq, Eq, Debug, Deserialize, Hash)] pub enum CooSystem { - ICRS, - GAL, + ICRS = 0, + GAL = 1, } pub const NUM_COOSYSTEM: usize = 2; diff --git a/src/core/al-api/src/hips.rs b/src/core/al-api/src/hips.rs index de1b3b75..a8e1ec2b 100644 --- a/src/core/al-api/src/hips.rs +++ b/src/core/al-api/src/hips.rs @@ -45,15 +45,15 @@ pub struct HiPSProperties { sky_fraction: Option, min_order: Option, - hips_initial_fov: Option, - hips_initial_ra: Option, - hips_initial_dec: Option, + initial_fov: Option, + initial_ra: Option, + initial_dec: Option, // HiPS cube - hips_cube_depth: Option, + cube_depth: Option, // HiPS 3D keywords - hips_order_freq: Option, - hips_tile_depth: Option, + order_freq: Option, + tile_depth: Option, /// Start of spectral coordinates (in meters) em_min: Option, @@ -77,11 +77,11 @@ pub struct HiPSProperties { impl HiPSProperties { #[inline(always)] pub fn get_hips_order_freq(&self) -> Option { - self.hips_order_freq + self.order_freq } #[inline(always)] pub fn get_hips_tile_depth(&self) -> Option { - self.hips_tile_depth + self.tile_depth } #[inline(always)] @@ -111,7 +111,7 @@ impl HiPSProperties { #[inline(always)] pub fn get_cube_depth(&self) -> Option { - self.hips_cube_depth + self.cube_depth } #[inline(always)] @@ -141,17 +141,17 @@ impl HiPSProperties { #[inline(always)] pub fn get_initial_fov(&self) -> Option { - self.hips_initial_fov + self.initial_fov } #[inline(always)] pub fn get_initial_ra(&self) -> Option { - self.hips_initial_ra + self.initial_ra } #[inline(always)] pub fn get_initial_dec(&self) -> Option { - self.hips_initial_dec + self.initial_dec } #[inline(always)] @@ -246,9 +246,13 @@ impl From for TransferFunction { } use crate::colormap::CmapLabel; -#[derive(Deserialize, Debug, Clone)] + +#[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct HiPSColor { +#[derive(Clone)] +#[wasm_bindgen] +pub struct ImageMetadata { + /// Color config // transfer function called before evaluating the colormap pub stretch: TransferFunction, // low cut @@ -258,26 +262,17 @@ pub struct HiPSColor { // flag to tell the colormap is queried reversed pub reversed: bool, // the colormap - pub cmap_name: CmapLabel, - /// tonal color tuning factors - pub k_gamma: f32, - pub k_saturation: f32, - pub k_contrast: f32, - pub k_brightness: f32, -} - -#[derive(Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -#[derive(Clone)] -#[wasm_bindgen] -pub struct ImageMetadata { - /// Color config #[wasm_bindgen(skip)] - pub color: HiPSColor, + pub colormap: CmapLabel, + /// tonal color tuning factors + pub gamma: f32, + pub saturation: f32, + pub contrast: f32, + pub brightness: f32, // Blending config #[serde(default)] - pub blend_cfg: BlendCfg, + pub blending: BlendCfg, #[serde(default = "default_opacity")] pub opacity: f32, /// the current format chosen @@ -289,90 +284,6 @@ fn default_opacity() -> f32 { } use crate::Abort; -#[wasm_bindgen] -impl ImageMetadata { - #[wasm_bindgen(setter = color)] - pub fn set_color(&mut self, color: JsValue) -> std::result::Result<(), JsValue> { - self.color = serde_wasm_bindgen::from_value(color)?; - - Ok(()) - } - - #[wasm_bindgen(getter = color)] - pub fn color(&self) -> JsValue { - let js_color_obj = js_sys::Object::new(); - - let HiPSColor { - stretch, - min_cut, - max_cut, - reversed, - cmap_name, - k_gamma, - k_saturation, - k_brightness, - k_contrast, - } = &self.color; - - js_sys::Reflect::set( - &js_color_obj, - &"stretch".into(), - &serde_wasm_bindgen::to_value(&stretch).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"minCut".into(), - &serde_wasm_bindgen::to_value(&min_cut).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"maxCut".into(), - &serde_wasm_bindgen::to_value(&max_cut).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"kGamma".into(), - &serde_wasm_bindgen::to_value(&k_gamma).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"kSaturation".into(), - &serde_wasm_bindgen::to_value(&k_saturation).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"kBrightness".into(), - &serde_wasm_bindgen::to_value(&k_brightness).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"kContrast".into(), - &serde_wasm_bindgen::to_value(&k_contrast).unwrap_abort(), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"reversed".into(), - &JsValue::from_bool(*reversed), - ) - .unwrap_abort(); - js_sys::Reflect::set( - &js_color_obj, - &"colormap".into(), - &serde_wasm_bindgen::to_value(&cmap_name).unwrap_abort(), - ) - .unwrap_abort(); - - js_color_obj.into() - } -} - impl ImageMetadata { pub fn visible(&self) -> bool { self.opacity > 0.0 diff --git a/src/core/al-core/Cargo.toml b/src/core/al-core/Cargo.toml index 5f65f281..d9d5b62c 100644 --- a/src/core/al-core/Cargo.toml +++ b/src/core/al-core/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] js-sys = "0.3.47" cgmath = "*" -fitsrs = "0.4.1" +fitsrs = { git = "https://github.com/cds-astro/fitsrs", branch = "master" } al-api = { path = "../al-api" } serde = { version = "^1.0.59", features = ["derive"] } serde_json = "1.0" diff --git a/src/core/al-core/src/shader.rs b/src/core/al-core/src/shader.rs index 4e57d058..d258b276 100644 --- a/src/core/al-core/src/shader.rs +++ b/src/core/al-core/src/shader.rs @@ -289,20 +289,9 @@ impl UniformType for TransferFunction { } } -use al_api::hips::HiPSColor; use al_api::hips::ImageMetadata; impl SendUniforms for ImageMetadata { - fn attach_uniforms<'a>(&self, shader: &'a ShaderBound<'a>) -> &'a ShaderBound<'a> { - shader - .attach_uniforms_from(&self.color) - .attach_uniform("opacity", &self.opacity); - - shader - } -} - -impl SendUniforms for HiPSColor { fn attach_uniforms<'a>(&self, shader: &'a ShaderBound<'a>) -> &'a ShaderBound<'a> { let reversed = self.reversed as u8 as f32; @@ -310,17 +299,18 @@ impl SendUniforms for HiPSColor { .attach_uniform("H", &self.stretch) .attach_uniform("min_value", &self.min_cut.unwrap_or(0.0)) .attach_uniform("max_value", &self.max_cut.unwrap_or(1.0)) - .attach_uniform("k_gamma", &self.k_gamma) - .attach_uniform("k_saturation", &self.k_saturation) - .attach_uniform("k_brightness", &self.k_brightness) - .attach_uniform("k_contrast", &self.k_contrast) - .attach_uniform("reversed", &reversed); + .attach_uniform("k_gamma", &self.gamma) + .attach_uniform("k_saturation", &self.saturation) + .attach_uniform("k_brightness", &self.brightness) + .attach_uniform("k_contrast", &self.contrast) + .attach_uniform("reversed", &reversed) + .attach_uniform("opacity", &self.opacity); shader } } -impl SendUniformsWithParams for HiPSColor { +impl SendUniformsWithParams for ImageMetadata { fn attach_uniforms_with_params<'a>( &self, shader: &'a ShaderBound<'a>, @@ -328,18 +318,19 @@ impl SendUniformsWithParams for HiPSColor { ) -> &'a ShaderBound<'a> { let reversed = self.reversed as u8 as f32; - let cmap = cmaps.get(self.cmap_name.as_ref()); + let cmap = cmaps.get(self.colormap.as_ref()); shader .attach_uniforms_from(cmaps) .attach_uniforms_with_params_from(cmap, cmaps) .attach_uniform("H", &self.stretch) .attach_uniform("min_value", &self.min_cut.unwrap_or(0.0)) .attach_uniform("max_value", &self.max_cut.unwrap_or(1.0)) - .attach_uniform("k_gamma", &self.k_gamma) - .attach_uniform("k_saturation", &self.k_saturation) - .attach_uniform("k_brightness", &self.k_brightness) - .attach_uniform("k_contrast", &self.k_contrast) - .attach_uniform("reversed", &reversed); + .attach_uniform("k_gamma", &self.gamma) + .attach_uniform("k_saturation", &self.saturation) + .attach_uniform("k_brightness", &self.brightness) + .attach_uniform("k_contrast", &self.contrast) + .attach_uniform("reversed", &reversed) + .attach_uniform("opacity", &self.opacity); shader } diff --git a/src/core/src/app.rs b/src/core/src/app.rs index 9c6d392c..236bb713 100644 --- a/src/core/src/app.rs +++ b/src/core/src/app.rs @@ -1452,8 +1452,8 @@ impl App { pub(crate) fn resize(&mut self, width: f32, height: f32) { self.camera.set_screen_size(width, height, &self.projection); - self.camera - .set_zoom_factor(self.camera.get_zoom_factor(), &self.projection); + //self.camera + // .set_zoom_factor(self.camera.get_zoom_factor(), &self.projection); // resize the view fbo //let screen_size = self.camera.get_screen_size(); @@ -1853,8 +1853,8 @@ impl App { self.request_redraw = true; } - pub(crate) fn get_fov(&self) -> f64 { - self.camera.get_aperture().to_degrees() + pub(crate) fn get_fov(&self) -> [f64; 2] { + [self.camera.get_aperture().to_degrees(), self.camera.get_aperture_y().to_degrees()] } pub(crate) fn get_colormaps(&self) -> &Colormaps { diff --git a/src/core/src/camera/viewport.rs b/src/core/src/camera/viewport.rs index 7661480b..78723b9f 100644 --- a/src/core/src/camera/viewport.rs +++ b/src/core/src/camera/viewport.rs @@ -24,6 +24,7 @@ const ZOOM_FACTOR_UPPER_LIMIT: f64 = 2.0; pub struct CameraViewPort { // The field of view angle aperture: f64, + aperture_y: f64, // The rotation of the camera center: Vector3, w2m_rot: Rotation, @@ -78,6 +79,9 @@ pub struct CameraViewPort { pub(crate) min_fov: Option, // an optional max field of view pub(crate) max_fov: Option, + + scissor_w: f64, + scissor_h: f64, } use al_api::coo_system::CooSystem; use al_core::WebGlContext; @@ -104,6 +108,7 @@ impl CameraViewPort { let last_user_action = UserAction::Starting; let aperture = projection.aperture_start().to_radians(); + let aperture_y = aperture; let w2m = Matrix3::identity(); let m2w = w2m; @@ -127,6 +132,9 @@ impl CameraViewPort { let width = width * dpi; let height = height * dpi; + let scissor_w = width as f64; + let scissor_h = height as f64; + let aspect = height / width; let ndc_to_clip = Vector2::new(1.0, (height as f64) / (width as f64)); let zoom_factor = 1.0; @@ -144,6 +152,8 @@ impl CameraViewPort { CameraViewPort { // The field of view angle aperture, + aperture_y, + center, // The rotation of the cameraq w2m_rot, @@ -187,6 +197,9 @@ impl CameraViewPort { min_fov: None, max_fov: None, + + scissor_w, + scissor_h } } @@ -246,7 +259,7 @@ impl CameraViewPort { } } - fn recompute_scissor(&self) { + fn recompute_scissor(&mut self) { // Clear all the screen before updating the scissor //self.gl.scissor(0, 0, self.width as i32, self.height as i32); //self.gl.clear(web_sys::WebGl2RenderingContext::COLOR_BUFFER_BIT); @@ -274,6 +287,9 @@ impl CameraViewPort { let w = (tr_s.x - tl_s.x).min(self.width as f64); let h = (br_s.y - tr_s.y).min(self.height as f64); + self.scissor_w = w; + self.scissor_h = h; + // Specify a scissor here self.gl.scissor( (tl_s.x as i32).max(0), @@ -284,6 +300,8 @@ impl CameraViewPort { } pub fn set_screen_size(&mut self, width: f32, height: f32, projection: &ProjectionType) { + let old_w = self.width; + let old_h = self.height; self.width = width * self.dpi; self.height = height * self.dpi; @@ -291,8 +309,9 @@ impl CameraViewPort { // Compute the new clip zoom factor self.compute_ndc_to_clip_factor(projection); - self.fov - .set_aperture(&self.ndc_to_clip, self.zoom_factor, &self.w2m, projection); + self.set_zoom_factor(self.zoom_factor * ((self.width / old_w) as f64), projection); + + //self.set_aperture(self.aperture * ((self.width / old_w) as f64), projection); let proj_area = projection.get_area(); self.is_allsky = !proj_area.is_in(&math::projection::ndc_to_clip_space( @@ -445,6 +464,12 @@ impl CameraViewPort { // Limit later the aperture to aperture_start self.aperture = aperture.min(aperture_start); + if self.scissor_h < (self.height - 1.0).into() { + self.aperture_y = aperture_start; + } else { + self.aperture_y = self.aperture / (self.aspect as f64); + } + // Project this vertex into the screen self.moved = true; self.zoomed = true; @@ -539,6 +564,12 @@ impl CameraViewPort { self.aperture = aperture; + if self.scissor_h < (self.height - 1.0).into() { + self.aperture_y = aperture_start; + } else { + self.aperture_y = self.aperture / (self.aspect as f64); + } + // Project this vertex into the screen self.moved = true; self.zoomed = true; @@ -610,7 +641,6 @@ impl CameraViewPort { Err(idx) => idx, }; - //al_core::log(&format!("{:?}", depth_pixel)); const DEPTH_OFFSET_TEXTURE: usize = 9; self.texture_depth = if DEPTH_OFFSET_TEXTURE > depth_pixel { 0_u8 @@ -799,6 +829,11 @@ impl CameraViewPort { self.aperture } + #[inline] + pub fn get_aperture_y(&self) -> f64 { + self.aperture_y + } + #[inline] pub fn get_center(&self) -> &Vector3 { &self.center diff --git a/src/core/src/lib.rs b/src/core/src/lib.rs index 8946969c..ad4dc860 100644 --- a/src/core/src/lib.rs +++ b/src/core/src/lib.rs @@ -465,9 +465,9 @@ impl WebClient { /// Get the field of the view in degrees #[wasm_bindgen(js_name = getFieldOfView)] - pub fn get_fov(&self) -> Result { + pub fn get_fov(&self) -> Result, JsValue> { let fov = self.app.get_fov(); - Ok(fov) + Ok(fov.into()) } /// Get the max aperture of a projection (in degrees) diff --git a/src/core/src/renderable/hips/d2/mod.rs b/src/core/src/renderable/hips/d2/mod.rs index 90bd5e4a..6a7bcddb 100644 --- a/src/core/src/renderable/hips/d2/mod.rs +++ b/src/core/src/renderable/hips/d2/mod.rs @@ -796,28 +796,27 @@ impl HiPS2D { let config = self.get_config(); let ImageMetadata { - color, opacity, - blend_cfg, + blending, + colormap, .. } = cfg; // Get the colormap from the color - let cmap = colormaps.get(color.cmap_name.as_ref()); + let colormap = colormaps.get(colormap.as_ref()); - blend_cfg.enable(&self.gl, || { + blending.enable(&self.gl, || { if draw_allsky { let w2v = c * (*camera.get_w2m()); - let shader = get_raytracer_shader(cmap, &self.gl, shaders, config)?; + let shader = get_raytracer_shader(colormap, &self.gl, shaders, config)?; let shader = shader.bind(&self.gl); shader .attach_uniforms_from(camera) .attach_uniforms_from(&self.buffer) // send the cmap appart from the color config - .attach_uniforms_with_params_from(cmap, colormaps) - .attach_uniforms_from(color) + .attach_uniforms_with_params_from(cfg, colormaps) .attach_uniform("model", &w2v) .attach_uniform("current_time", &utils::get_current_time()) .attach_uniform( @@ -851,13 +850,13 @@ impl HiPS2D { // - The UVs are changed if: // * new cells are added/removed (because new cells are added) // * there are new available tiles for the GPU - let shader = get_raster_shader(cmap, &self.gl, shaders, config)?.bind(&self.gl); + let shader = get_raster_shader(colormap, &self.gl, shaders, config)?.bind(&self.gl); shader .attach_uniforms_from(&self.buffer) // send the cmap appart from the color config - .attach_uniforms_with_params_from(cmap, colormaps) - .attach_uniforms_from(color) + .attach_uniforms_with_params_from(colormap, colormaps) + .attach_uniforms_from(cfg) .attach_uniforms_from(camera) .attach_uniform("inv_model", &v2w) .attach_uniform("current_time", &utils::get_current_time()) diff --git a/src/core/src/renderable/hips/d3/mod.rs b/src/core/src/renderable/hips/d3/mod.rs index 7e1760b4..cfacbc2c 100644 --- a/src/core/src/renderable/hips/d3/mod.rs +++ b/src/core/src/renderable/hips/d3/mod.rs @@ -1064,13 +1064,13 @@ impl HiPS3D { } let ImageMetadata { - color, + colormap, opacity, - blend_cfg, + blending, .. } = cfg; - let cmap = colormaps.get(color.cmap_name.as_ref()); + let colormap = colormaps.get(colormap.as_ref()); let v2w = (*camera.get_m2w()) * c.transpose(); @@ -1090,7 +1090,7 @@ impl HiPS3D { let shader = get_raster_shader(&self.gl, shaders, hips_cfg)?; for (cell, num_indices) in self.cells.iter().zip(self.num_indices.iter()) { - blend_cfg.enable(&self.gl, || { + blending.enable(&self.gl, || { // Bind the shader at each draw of a cell to not exceed the max number of tex image units bindable // to a shader. It is 32 in my case let shaderbound = shader.bind(&self.gl); @@ -1098,8 +1098,8 @@ impl HiPS3D { shaderbound .attach_uniform("tex", &self.buffer.get(cell).unwrap_abort().texture) .attach_uniforms_from(&self.buffer) - .attach_uniforms_with_params_from(cmap, colormaps) - .attach_uniforms_from(color) + .attach_uniforms_with_params_from(colormap, colormaps) + .attach_uniforms_from(cfg) .attach_uniforms_from(camera) .attach_uniform("inv_model", &v2w) .attach_uniform("opacity", opacity) diff --git a/src/core/src/renderable/image/mod.rs b/src/core/src/renderable/image/mod.rs index a6ee5dee..fbd3b330 100644 --- a/src/core/src/renderable/image/mod.rs +++ b/src/core/src/renderable/image/mod.rs @@ -722,9 +722,8 @@ impl Image { //self.gl.enable(WebGl2RenderingContext::BLEND); let ImageMetadata { - color, opacity, - blend_cfg, + blending, .. } = cfg; @@ -758,7 +757,7 @@ impl Image { //self.gl.disable(WebGl2RenderingContext::CULL_FACE); // 2. Draw it if its opacity is not null - blend_cfg.enable(&self.gl, || { + blending.enable(&self.gl, || { let mut off_indices = 0; for &idx_tex in self.idx_tex.iter() { let texture = &self.textures[idx_tex]; @@ -767,7 +766,7 @@ impl Image { let shader_bound = shader.bind(&self.gl); shader_bound - .attach_uniforms_with_params_from(color, colormaps) + .attach_uniforms_with_params_from(cfg, colormaps) .attach_uniform("opacity", opacity) .attach_uniform("tex", texture) .attach_uniform("scale", &self.bscale) diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index 41cf5a90..e8b954ae 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -3,19 +3,717 @@ use std::collections::HashMap; pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); out.insert( - r"colormaps_colormap.vert", + r"catalogs_ortho.frag", r#"#version 300 es precision lowp float; -precision lowp sampler2D; -layout (location = 0) in vec2 position; +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + void main() { - gl_Position = vec4(position, 0.f, 1.f); + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.frag", + r#"#version 300 es +precision lowp float; +precision mediump int; + +out vec4 out_frag_color; + +uniform vec3 color; + +void main() { + out_frag_color = vec4(color, 1.0); +}"#, + ); + out.insert( + r"catalogs_healpix.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_healpix(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"hips_rasterizer_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i16(uv0); + vec4 color_end = uvw2c_i16(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_raster.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; + +out vec3 frag_uv; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv = uv; }"#, ); out.insert( @@ -412,6 +1110,1406 @@ void main() { out_frag_color = c; out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_aitoff.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_aitoff(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); + out_uv = uv; +}"#, + ); + out.insert( + r"fits_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i16(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_f32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips_raytracer_raytracer.vert", + r#"#version 300 es +precision lowp float; +precision mediump int; + +layout (location = 0) in vec2 pos_clip_space; +layout (location = 1) in vec3 pos_world_space; + +out vec2 out_clip_pos; +out vec3 frag_pos; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform mat3 model; + +void main() { + vec2 uv = pos_clip_space * 0.5 + 0.5; + + frag_pos = model * pos_world_space; + + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); + out_clip_pos = pos_clip_space; +}"#, + ); + out.insert( + r"moc_base.frag", + r#"#version 300 es + +precision lowp float; +out vec4 color; + +uniform vec4 u_color; + +void main() { + color = u_color; +}"#, + ); + out.insert( + r"catalogs_catalog.frag", + r#"#version 300 es +precision lowp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"catalogs_arc.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_arc(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"fits_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"line_base.frag", + r#"#version 300 es + +precision highp float; + +out vec4 color; +in vec2 l; + +uniform vec4 u_color; +uniform float u_thickness; +uniform float u_width; +uniform float u_height; + +void main() { + if (l.x > 0.05) { + discard; + } else { + color = u_color; + + float dist = abs((u_thickness + 2.0) * l.y); + + float half_thickness = (u_thickness + 2.0) * 0.5; + color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); + } +}"#, + ); + out.insert( + r"catalogs_mercator.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mercator(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"passes_post_fragment_100es.frag", + r#"#version 300 es +precision mediump float; + +in vec2 v_tc; +out vec4 color; + +uniform sampler2D fbo_tex; + +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); +} + +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); +} + +void main() { + color = texture(fbo_tex, v_tc); + }"#, ); out.insert( @@ -430,19 +2528,148 @@ void main() { }"#, ); out.insert( - r"fits_base.vert", + r"line_inst_lonlat.vert", r#"#version 300 es precision highp float; -precision highp int; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; +layout (location = 2) in vec2 vertex; -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; -out vec2 frag_uv; +out vec2 l; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); + + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); }"#, ); out.insert( @@ -709,3700 +2936,6 @@ void main() { out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_u8(uv0); - vec4 color_end = uvw2c_u8(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i16(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_raytracer.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; -layout (location = 1) in vec3 pos_world_space; - -out vec2 out_clip_pos; -out vec3 frag_pos; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform mat3 model; - -void main() { - vec2 uv = pos_clip_space * 0.5 + 0.5; - - frag_pos = model * pos_world_space; - - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); - out_clip_pos = pos_clip_space; -}"#, - ); - out.insert( - r"catalogs_mollweide.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mollweide(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_aitoff.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_aitoff(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_raytracer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec2 out_clip_pos; -in vec3 frag_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -uniform float opacity; -uniform vec4 no_tile_color; - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2c_rgba(uv); - - out_frag_color = c; - out_frag_color = vec4(c.rgb, opacity * c.a); -}"#, - ); - out.insert( - r"moc_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 lonlat; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); -}"#, - ); - out.insert( - r"moc_base.frag", - r#"#version 300 es - -precision lowp float; -out vec4 color; - -uniform vec4 u_color; - -void main() { - color = u_color; -}"#, - ); - out.insert( - r"catalogs_tan.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_gnomonic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); -}"#, - ); - out.insert( - r"catalogs_catalog.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"hips3d_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_u8(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2cmap_rgba(frag_uv_start); - vec4 color_end = uvw2cmap_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"line_inst_lonlat.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; -layout (location = 2) in vec2 vertex; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -out vec2 l; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); - - vec2 da = p_a_clip - p_b_clip; - - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); -}"#, - ); - out.insert( - r"hips_rasterizer_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i16(uv0); - vec4 color_end = uvw2c_i16(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"passes_post_vertex_100es.vert", - r#"#version 300 es -precision mediump float; - -layout (location = 0) in vec2 a_pos; -out vec2 v_tc; - -void main() { - gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); - v_tc = a_pos; -}"#, - ); - out.insert( - r"line_inst_ndc.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; -layout (location = 2) in vec2 vertex; - -out vec2 l; - -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -void main() { - vec2 x_b = p_b - p_a; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); -}"#, - ); - out.insert( - r"line_base.frag", - r#"#version 300 es - -precision highp float; - -out vec4 color; -in vec2 l; - -uniform vec4 u_color; -uniform float u_thickness; -uniform float u_width; -uniform float u_height; - -void main() { - if (l.x > 0.05) { - discard; - } else { - color = u_color; - - float dist = abs((u_thickness + 2.0) * l.y); - - float half_thickness = (u_thickness + 2.0) * 0.5; - color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); - } -}"#, - ); - out.insert( - r"hips_rasterizer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i32(uv0); - vec4 color_end = uvw2c_i32(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; - -uniform vec2 ndc_to_clip; -uniform float czf; - -void main() { - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float current_time; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); -}"#, - ); - out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.frag", - r#"#version 300 es -precision lowp float; -precision mediump int; - -out vec4 out_frag_color; - -uniform vec3 color; - -void main() { - out_frag_color = vec4(color, 1.0); }"#, ); out.insert( @@ -4803,22 +3336,14 @@ void main() { }"#, ); out.insert( - r"catalogs_ortho.vert", + r"moc_base.vert", r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; +precision highp float; +layout (location = 0) in vec2 lonlat; +uniform mat3 u_2world; uniform vec2 ndc_to_clip; uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; const float PI = 3.141592653589793; const float SQRT_2 = 1.41421356237309504880168872420969808; @@ -4921,30 +3446,37 @@ vec2 proj(vec3 p) { } } - void main() { - vec3 p = inv_model * center; + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); - vec2 center_pos_clip_space = world2clip_orthographic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.f, 1.f); }"#, ); out.insert( - r"fits_i16.frag", + r"hips_raytracer_i16.frag", r#"#version 300 es precision lowp float; -precision lowp sampler2D; +precision lowp sampler2DArray; precision mediump int; -out vec4 out_frag_color; -in vec2 frag_uv; +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; -uniform sampler2D tex; uniform float opacity; uniform float scale; @@ -5047,6 +3579,21 @@ vec4 apply_tonal(vec4 color) { k_gamma ); } +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} highp float decode_f32(highp vec4 rgba) { highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; @@ -5090,6 +3637,49 @@ uint decode_u8(float r) { +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} vec4 val2c_f32(float x) { float alpha = x * scale + offset; @@ -5111,32 +3701,1864 @@ vec4 val2c(float x) { return apply_tonal(new_color); } -vec4 uv2c_f32(vec2 uv) { +vec4 uvw2c_f32(vec3 uv) { float val = decode_f32(texture(tex, uv).rgba*255.0); return val2c_f32(val); } -vec4 uv2c_i32(vec2 uv) { +vec4 uvw2c_i32(vec3 uv) { float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); } -vec4 uv2c_i16(vec2 uv) { +vec4 uvw2c_i16(vec3 uv) { float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i16(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } -vec4 uv2c_u8(vec2 uv) { +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_u8(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"image_sampler.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +void main() { + out_frag_color = texture(tex, frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_red.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } void main() { - vec2 uv = frag_uv; + vec3 uv = vec3(frag_uv.xyz); + vec4 color = uvw2c_ra(uv); + + out_frag_color = color; + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips_raytracer_rgba2cmap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp sampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2cmap_rgba(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); uv.y = 1.0 - uv.y; - out_frag_color = uv2c_i16(frag_uv); + vec4 color = uvw2c_i32(uv); + + out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_rasterizer_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i32(uv0); + vec4 color_end = uvw2c_i32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_base.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"line_inst_ndc.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; +layout (location = 2) in vec2 vertex; + +out vec2 l; + +uniform float u_width; +uniform float u_height; +uniform float u_thickness; + +void main() { + vec2 x_b = p_b - p_a; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); +}"#, + ); + out.insert( + r"passes_post_vertex_100es.vert", + r#"#version 300 es +precision mediump float; + +layout (location = 0) in vec2 a_pos; +out vec2 v_tc; + +void main() { + gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); + v_tc = a_pos; +}"#, + ); + out.insert( + r"colormaps_colormap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +in vec2 out_uv; +out vec4 color; + +uniform sampler2D texture_fbo; +uniform float alpha; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} + +void main() { + float opacity = texture(texture_fbo, out_uv).r; + + float o = smoothstep(0.0, 0.1, opacity); + + color = colormap_f(opacity); + color.a = o * alpha; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.vert", + r#"#version 300 es +precision lowp float; +precision mediump int; + +layout (location = 0) in vec2 pos_clip_space; + +uniform vec2 ndc_to_clip; +uniform float czf; + +void main() { + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); +}"#, + ); + out.insert( + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); }"#, ); out.insert( @@ -5345,20 +5767,268 @@ void main() { }"#, ); out.insert( - r"image_sampler.frag", + r"hips_rasterizer_rgba2cmap.frag", r#"#version 300 es -precision highp float; -precision highp sampler2D; +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; uniform float opacity; +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + void main() { - out_frag_color = texture(tex, frag_uv); - out_frag_color.a = out_frag_color.a * opacity; + vec4 color_start = uvw2cmap_rgba(frag_uv_start); + vec4 color_end = uvw2cmap_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; }"#, ); out.insert( @@ -5762,946 +6432,7 @@ void main() { }"#, ); out.insert( - r"catalogs_mercator.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mercator(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; - -out vec3 frag_uv; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; -}"#, - ); - out.insert( - r"colormaps_colormap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -in vec2 out_uv; -out vec4 color; - -uniform sampler2D texture_fbo; -uniform float alpha; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} - -void main() { - float opacity = texture(texture_fbo, out_uv).r; - - float o = smoothstep(0.0, 0.1, opacity); - - color = colormap_f(opacity); - color.a = o * alpha; -}"#, - ); - out.insert( - r"catalogs_healpix.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_healpix(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"fits_f32.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_f32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision mediump float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - -}"#, - ); - out.insert( - r"hips3d_i32.frag", + r"hips3d_i16.frag", r#"#version 300 es precision lowp float; precision lowp sampler3D; @@ -6962,275 +6693,10 @@ void main() { vec3 uv = vec3(frag_uv.xyz); uv.y = 1.0 - uv.y; - vec4 color = uvw2c_i32(uv); + vec4 color = uvw2c_i16(uv); out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_red.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = vec3(frag_uv.xyz); - vec4 color = uvw2c_ra(uv); - - out_frag_color = color; - out_frag_color.a = opacity * out_frag_color.a; }"#, ); out.insert( @@ -7505,7 +6971,543 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_i16.frag", + r"catalogs_ortho.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_orthographic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_rasterizer_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_u8(uv0); + vec4 color_end = uvw2c_u8(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_tan.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_gnomonic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_raytracer_rgba.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; @@ -7513,8 +7515,8 @@ precision mediump int; uniform sampler2DArray tex; -in vec3 frag_pos; in vec2 out_clip_pos; +in vec3 frag_pos; out vec4 out_frag_color; struct Tile { @@ -7526,8 +7528,6 @@ struct Tile { uniform Tile textures_tiles[12]; -uniform float opacity; - uniform float scale; uniform float offset; uniform float blank; @@ -7888,15 +7888,15 @@ vec3 xyz2uv(vec3 xyz) { return vec3(offset, float(tile.texture_idx)); } +uniform float opacity; +uniform vec4 no_tile_color; + void main() { vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i16(uv); - + vec4 c = uvw2c_rgba(uv); out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; + out_frag_color = vec4(c.rgb, opacity * c.a); }"#, ); out diff --git a/src/css/aladin.css b/src/css/aladin.css index cbde36fa..d07c1a6d 100644 --- a/src/css/aladin.css +++ b/src/css/aladin.css @@ -3,7 +3,7 @@ border: 0px solid #ddd; /* SVG inside divs add a 4px height: https://stackoverflow.com/questions/75751593/why-there-is-additional-4px-height-for-div-when-there-is-svg-inside-it */ /* disable x swipe on chrome, firefox */ - /* see. https://stackoverflow.com/questions/30636930/disable-web-page-navigation-on-swipeback-and-forward */ + /* Prevent page navigation swipe on windows phone: https://stackoverflow.com/questions/30636930/disable-web-page-navigation-on-swipeback-and-forward */ overscroll-behavior-x: none; overscroll-behavior-y: none; /* Prevents vertical pull-to-refresh */ /* Hide the draggable boxes that goes out of the view */ @@ -15,7 +15,7 @@ font-size: 0.9rem; /* Aladin lite default color */ - --aladin-color: #b232b2; + --aladin-color: #ff54ff; } [data-theme="dark"] { @@ -114,27 +114,30 @@ background: black; } -.aladin-lite-spectra-displayer .aladin-spectra-unit-selector { +.aladin-spectra-displayer { position: absolute; - bottom: 3rem; - right: 0; + left: 50%; + transform: translateX(-50%); + bottom: 0; + overflow: hidden; + width: 100%; } -.aladin-lite-spectra-displayer .aladin-spectra-home { + +.aladin-spectra-displayer .aladin-spectra-unit { position: absolute; - bottom: 5rem; - right: 0; + bottom: 0; + right: 8rem; } -.aladin-lite-spectra-displayer .aladin-spectra-extraction { +.aladin-spectra-displayer .aladin-spectra-home { + position: absolute; + bottom: 0; + right: 6rem; +} +.aladin-spectra-displayer .aladin-spectra-extraction { position: absolute; bottom: 7rem; right: 0; } -.aladin-lite-spectra-displayer .aladin-spectra-hips-selector { - position: absolute; - top: 0; - left: 0; - max-width: 10rem; -} .aladin-imageCanvas { position: absolute; @@ -1302,14 +1305,21 @@ otherwise it fits its content options. If those are too big the select can go ou line-height: 1.7rem; } -.aladin-fov .aladin-zoom-out { - margin-right: 0; - border-right: none; - border-radius: 0.4rem 0px 0px 0.4rem; +.aladin-zoom { + position: absolute; + top: 50%; + right: 0; + transform: translateY(-50%); } -.aladin-fov .aladin-zoom-in { - border-radius: 0px 0.4rem 0.4rem 0px; +.aladin-zoom-out { + margin-right: 0; + border-top: none; + border-radius: 0 0 0.4rem 0.4rem; +} + +.aladin-zoom-in { + border-radius: 0.4rem 0.4rem 0 0; } .aladin-status-bar { @@ -1348,6 +1358,7 @@ otherwise it fits its content options. If those are too big the select can go ou position: absolute; margin-top: 3rem; margin-bottom: 5rem; + left: 0.2rem; } .aladin-widgets-toolbar > * { diff --git a/src/js/Aladin.js b/src/js/Aladin.js index ffda5223..853f9335 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -407,7 +407,6 @@ export let Aladin = (function () { // Aladin logo new AladinLogo(this.aladinDiv); - this.reticle = new Reticle(this.options, this); this.popup = new Popup(this.aladinDiv, this.view); this.tooltip = document.createElement('div') @@ -453,7 +452,7 @@ export let Aladin = (function () { } // Format the hipslist given by the user before storing it in the aladin instance - this.hipsFavorites = []; + this.layerFavorites = []; let hipsList = [].concat(options.hipsList); for (var hips of hipsList) { @@ -498,9 +497,11 @@ export let Aladin = (function () { hipsObj["name"] = name; } + hipsObj["type"] = "hips"; + // Merge what is already in the cache for that HiPS with new properties // coming from the MOCServer - this.hipsFavorites.push(hipsObj); + this.layerFavorites.push(hipsObj); // Favorites are also directly pushed to the cache this.hipsCache.append(hipsObj.id, hipsObj) } @@ -511,7 +512,7 @@ export let Aladin = (function () { this._setupUI(options); - ALEvent.FAVORITE_HIPS_LIST_UPDATED.dispatchedTo(document, this.hipsFavorites); + ALEvent.FAVORITE_LAYERS_LIST_UPDATED.dispatchedTo(document, this.layerFavorites); if (options.survey) { if (Array.isArray(options.survey)) { @@ -759,9 +760,6 @@ export let Aladin = (function () { /**** CONSTANTS ****/ Aladin.VERSION = version; - Aladin.JSONP_PROXY = "https://alaskybis.cds.unistra.fr/cgi/JSONProxy"; - //Aladin.JSONP_PROXY = "https://alaskybis.unistra.fr/cgi/JSONProxy"; - Aladin.URL_PREVIEWER = "https://aladin.cds.unistra.fr/AladinLite/"; // access to WASM libraries @@ -804,10 +802,10 @@ export let Aladin = (function () { showCatalog: true, // TODO: still used ?? fullScreen: false, - reticleColor: "rgb(178, 50, 178)", + reticleColor: "#ff54ff", reticleSize: 22, - gridColor: "rgb(178, 50, 178)", - gridOpacity: 0.8, + gridColor: "#ff54ff", + gridOpacity: 1.0, gridOptions: { enabled: false, showLabels: true, @@ -1751,7 +1749,7 @@ export let Aladin = (function () { maxOrder, options ) { - let hipsOptions = { id, name, maxOrder, url, cooFrame, ...options }; + let hipsOptions = { id, name, maxOrder, url, cooFrame, type: "hips", ...options }; let hips = new HiPS(id, url || id, hipsOptions) // A HiPS can be refered by its unique ID thus we add it to the cache (cf excample/al-cfht.html that refers to HiPS object just by their unique ID) @@ -1787,7 +1785,7 @@ export let Aladin = (function () { * Remove a HiPS from the list of favorites. * * This send a event of type - * FAVORITE_HIPS_LIST_UPDATED which can be listened to + * FAVORITE_LAYERS_LIST_UPDATED which can be listened to * * @throws A warning when the asset is currently present in the view * @@ -1807,7 +1805,7 @@ export let Aladin = (function () { } // find the index of the hips to remove - const idx = this.hipsFavorites.findIndex((hipsObj) => { + const idx = this.layerFavorites.findIndex((hipsObj) => { if (typeof hips !== "string") { return hipsObj.name == hips.name || hipsObj.id == hips.id || hipsObj.url == hips.url; } else { @@ -1817,9 +1815,9 @@ export let Aladin = (function () { // a hips matches if (idx >= 0) { - this.hipsFavorites.splice(idx, 1); + this.layerFavorites.splice(idx, 1); // Send a change of favorites for the UI selector to adapt their optional list - ALEvent.FAVORITE_HIPS_LIST_UPDATED.dispatchedTo(document, this.hipsFavorites); + ALEvent.FAVORITE_LAYERS_LIST_UPDATED.dispatchedTo(document, this.layerFavorites); } } @@ -1827,19 +1825,19 @@ export let Aladin = (function () { * Add a HiPS to the list of favorites. * * If already present it will not add it again. This send a event of type - * FAVORITE_HIPS_LIST_UPDATED which can be listened to. Once added, the favorite list + * FAVORITE_LAYERS_LIST_UPDATED which can be listened to. Once added, the favorite list * will be sorted by the name of the hips. * * @memberof Aladin - * @param {HiPS} hips - The HiPS to add to the favorites + * @param {HiPS|Image} layer - The HiPS to add to the favorites */ - Aladin.prototype.addHiPSToFavorites = function(hips) { + Aladin.prototype.addHiPSToFavorites = function(layer) { // find the index of the hips to remove - const idx = this.hipsFavorites.findIndex((hipsObj) => { - if (typeof hips !== "string") { - return hipsObj.name == hips.name || hipsObj.id == hips.id || hipsObj.url == hips.url; + const idx = this.layerFavorites.findIndex((obj) => { + if (typeof layer !== "string") { + return obj.id == layer.id; } else { - return hipsObj.name == hips || hipsObj.id == hips || hipsObj.url == hips; + return obj.id == layer; } }) @@ -1849,14 +1847,14 @@ export let Aladin = (function () { } // add the new favorite HiPS - this.hipsFavorites.push({ - url: hips.url, - id: hips.id, - name: hips.name, + this.layerFavorites.push({ + url: layer.url, + id: layer.id, + name: layer.name, }) // send the final event - ALEvent.FAVORITE_HIPS_LIST_UPDATED.dispatchedTo(document, this.hipsFavorites); + ALEvent.FAVORITE_LAYERS_LIST_UPDATED.dispatchedTo(document, this.layerFavorites); } /** @@ -1982,6 +1980,11 @@ export let Aladin = (function () { Aladin.prototype.addNewImageLayer = function (survey = "P/DSS2/color") { let layerName = Utils.uuidv4(); return this.setOverlayImageLayer(survey, layerName); + //let newHiPS = A.HiPS(survey); + //newHiPS.id = this.hipsCache.makeUniqLayerName(newHiPS.id); + //newHiPS.name = newHiPS.id; + + //return this.setOverlayImageLayer(newHiPS, layerName); }; /** @@ -2080,7 +2083,14 @@ export let Aladin = (function () { * */ Aladin.prototype.setBaseImageLayer = function (urlOrHiPSOrFITS) { - return this.setOverlayImageLayer(urlOrHiPSOrFITS, (this.view.overlayLayers && this.view.overlayLayers[0]) || Utils.uuidv4()); + /*if (this.view._waitsForLayer()) { + // delay this call + this.view.delayedBaseLayerCalledParams = urlOrHiPSOrFITS; + return; + }*/ + let firstLayer = this.view.getFirstLayer(); + + return this.setOverlayImageLayer(urlOrHiPSOrFITS, firstLayer || Utils.uuidv4()); }; /** @@ -2090,7 +2100,9 @@ export let Aladin = (function () { * @returns {HiPS|Image} - Returns the image layer corresponding to the base layer */ Aladin.prototype.getBaseImageLayer = function () { - return this.view.getImageLayer(this.view.overlayLayers && this.view.overlayLayers[0]); + let firstLayer = this.view.getFirstLayer(); + + return this.view.getImageLayer(firstLayer); }; /** @@ -2108,7 +2120,7 @@ export let Aladin = (function () { * on top the 'base' layer. If the layer is already present in the view, it will be replaced by the new HiPS/FITS image given here. */ Aladin.prototype.setOverlayImageLayer = function ( - urlOrHiPSOrFITS, + urlOrHiPSOrImage, layer = "overlay" ) { let imageLayer; @@ -2116,14 +2128,17 @@ export let Aladin = (function () { let hipsCache = this.hipsCache; // 1. User gives an ID - if (typeof urlOrHiPSOrFITS === "string") { - const idOrUrl = urlOrHiPSOrFITS; + if (typeof urlOrHiPSOrImage === "string") { + const idOrUrl = urlOrHiPSOrImage; // many cases here // 1/ It has been already added to the cache let cachedOptions = hipsCache.get(idOrUrl) - if (cachedOptions) { - imageLayer = A.HiPS(idOrUrl, cachedOptions); + if (cachedOptions.type === "hips") { + imageLayer = A.HiPS(idOrUrl, cachedOptions) + } else if (cachedOptions.type === "image") { + imageLayer = A.image(idOrUrl, cachedOptions); + } } else { // 2/ Not in the cache, then we create the hips from this url/id and // go to the case 3 @@ -2133,17 +2148,20 @@ export let Aladin = (function () { } } else { // 3/ It is an image survey. - imageLayer = urlOrHiPSOrFITS; + imageLayer = urlOrHiPSOrImage; - if (imageLayer instanceof HiPS) { - let cachedLayerOptions = hipsCache.get(imageLayer.id) + let cachedLayerOptions = hipsCache.get(imageLayer.id) - if (!cachedLayerOptions) { - hipsCache.append(imageLayer.id, imageLayer.options) - } else { - // Set the image layer object with the options from the cache. - imageLayer.setOptions(cachedLayerOptions) + if (!cachedLayerOptions) { + let type = "hips"; + if (imageLayer instanceof Image) { + type = "image"; } + + hipsCache.append(imageLayer.id, {...imageLayer.options, type}) + } else { + // Set the image layer object with the options from the cache. + imageLayer.setOptions(cachedLayerOptions) } } @@ -2777,18 +2795,18 @@ export let Aladin = (function () { */ Aladin.prototype.pix2world = function (x, y, frame) { if (frame) { - frame = CooFrameEnum.fromString(frame, CooFrameEnum.ICRS); + if (typeof frame === "string") { + frame = CooFrameEnum.fromString(frame, CooFrameEnum.ICRS); + } + + // Map to the numeric wasm-bindgen CooSystem discriminant + frame = (frame.system === CooFrameEnum.SYSTEMS.GAL) + ? Aladin.wasmLibs.core.CooSystem.GAL + : Aladin.wasmLibs.core.CooSystem.ICRS; } + let [lon, lat] = this.view.wasm.pix2world(x, y, frame); - let lonlat = this.view.wasm.pix2world(x, y, frame && frame.system); - - let [lon, lat] = lonlat; - - if (lon < 0) { - return [lon + 360.0, lat]; - } - - return [lon, lat]; + return [lon < 0 ? lon + 360.0 : lon, lat]; }; /** @@ -2805,17 +2823,17 @@ export let Aladin = (function () { */ Aladin.prototype.world2pix = function (lon, lat, frame) { if (frame) { - if (frame instanceof string) { + if (typeof frame === "string") { frame = CooFrameEnum.fromString(frame, CooFrameEnum.ICRS); } - if (frame.label == CooFrameEnum.SYSTEMS.GAL) { - frame = Aladin.wasmLibs.core.CooSystem.GAL; - } - else { - frame = Aladin.wasmLibs.core.CooSystem.ICRS; - } + // Map to the numeric wasm-bindgen CooSystem discriminant + frame = (frame.system === CooFrameEnum.SYSTEMS.GAL) + ? Aladin.wasmLibs.core.CooSystem.GAL + : Aladin.wasmLibs.core.CooSystem.ICRS; } + // frame === undefined/null → passed as-is, WASM treats as None (default system) + return this.view.wasm.world2pix(lon, lat, frame); }; @@ -2891,10 +2909,7 @@ export let Aladin = (function () { */ Aladin.prototype.getFov = function () { var fovX = this.view.fov; - var s = this.getSize(); - - var fovY = (s[1] / s[0]) * fovX; - fovY = Math.min(fovY, 180); + var fovY = this.view.fovY; return [fovX, fovY]; }; diff --git a/src/js/Catalog.js b/src/js/Catalog.js index d0731f18..68f0a1ec 100644 --- a/src/js/Catalog.js +++ b/src/js/Catalog.js @@ -517,7 +517,8 @@ export let Catalog = (function () { var cacheMarkerCtx = this.cacheMarkerCanvas.getContext("2d"); cacheMarkerCtx.fillStyle = this.color; cacheMarkerCtx.beginPath(); - var half = this.markerSize / 2; + var half = Math.max(this.markerSize / 2, 2.0); + cacheMarkerCtx.arc(half, half, half - 2, 0, 2 * Math.PI, false); cacheMarkerCtx.fill(); cacheMarkerCtx.lineWidth = 2; diff --git a/src/js/ColorCfg.js b/src/js/ColorCfg.js deleted file mode 100644 index f8833510..00000000 --- a/src/js/ColorCfg.js +++ /dev/null @@ -1,291 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-later -// Copyright 2013 - UDS/CNRS -// The Aladin Lite program is distributed under the terms -// of the GNU Lesser General Public License version 3 -// or (at your option) any later version. -// -// This file is part of Aladin Lite. -// -// Aladin Lite is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Aladin Lite is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with Aladin Lite. If not, see . -// - -/****************************************************************************** - * Aladin Lite project - * - * File ColorCfg - * - * Authors: Thomas Boch & Matthieu Baumann [CDS] - * - *****************************************************************************/ - export let ColorCfg = (function() { - /** Constructor - * cooFrame and maxOrder can be set to null - * They will be determined by reading the properties file - */ - function ColorCfg(options) { - // Opacity of the survey/image - this.opacity = (options && options.opacity) || 1.0; - - // Colormap config options - this.colormap = (options && options.colormap) || "native"; - this.colormap = this.colormap.toLowerCase(); - - this.stretch = (options && options.stretch) || "linear"; - this.stretch = this.stretch.toLowerCase(); - this.reversed = false; - // Keep the image tile format because we want the cuts - this.imgFormat = options.imgFormat || 'png'; - - if (options && options.reversed === true) { - this.reversed = true; - } - - this.minCut = { - webp: 0.0, - jpeg: 0.0, - png: 0.0, - fits: undefined // wait the default value coming from the properties - }; - if (options && Number.isFinite(options.minCut)) { - this.minCut[this.imgFormat] = options.minCut; - } - - this.maxCut = { - webp: 255.0, - jpeg: 255.0, - png: 255.0, - fits: undefined // wait the default value coming from the properties - }; - if (options && Number.isFinite(options.maxCut)) { - this.maxCut[this.imgFormat] = options.maxCut; - } - - this.additiveBlending = options && options.additive; - if (this.additiveBlending === undefined) { - this.additiveBlending = false; - } - - // A default value for gamma correction - this.kGamma = (options && options.gamma) || 1.0; - this.kSaturation = (options && options.saturation) || 0.0; - this.kBrightness = (options && options.brightness) || 0.0; - this.kContrast = (options && options.contrast) || 0.0; - }; - - ColorCfg.prototype.get = function() { - let blend = { - srcColorFactor: 'SrcAlpha', - dstColorFactor: 'OneMinusSrcAlpha', - func: 'FuncAdd' - }; - - if (this.additiveBlending) { - blend = { - srcColorFactor: 'SrcAlpha', - dstColorFactor: 'One', - func: 'FuncAdd' - } - } - - let minCut = this.minCut[this.imgFormat] - if (this.imgFormat !== "fits") { - minCut /= 255.0 - } - - let maxCut = this.maxCut[this.imgFormat] - if (this.imgFormat !== "fits") { - maxCut /= 255.0 - } - - // Reset the whole meta object - return { - blendCfg: blend, - opacity: this.opacity, - color: { - // Tonal corrections constants - kGamma: this.kGamma, - kSaturation: this.kSaturation, - kBrightness: this.kBrightness, - kContrast: this.kContrast, - - stretch: this.stretch, - minCut, - maxCut, - reversed: this.reversed, - cmapName: this.colormap, - } - }; - } - - ColorCfg.prototype.setOptions = function(options) { - // Update the imgFormat - this.imgFormat = options.imgFormat || this.imgFormat; - - this.setColormap(options.colormap, options) - - this.setCuts(options.minCut, options.maxCut, options.cutFormat) - - this.setBrightness(options.brightness) - this.setSaturation(options.saturation) - this.setContrast(options.contrast) - - this.setGamma(options.gamma) - - this.setOpacity(options.opacity) - - this.setBlendingConfig(options.additive) - } - - // @api - ColorCfg.prototype.setBrightness = function(kBrightness) { - if (kBrightness == null || kBrightness == undefined) - return; - - kBrightness = +kBrightness || 0.0; // coerce to number - this.kBrightness = Math.max(-1, Math.min(kBrightness, 1)); - }; - - // @api - ColorCfg.prototype.getBrightness = function() { - return this.kBrightness; - }; - - // @api - ColorCfg.prototype.setContrast = function(kContrast) { - if (kContrast == null || kContrast == undefined) - return; - - kContrast = +kContrast || 0.0; // coerce to number - this.kContrast = Math.max(-1, Math.min(kContrast, 1)); - }; - - // @api - ColorCfg.prototype.getContrast = function() { - return this.kContrast; - }; - - // @api - ColorCfg.prototype.setSaturation = function(kSaturation) { - if (kSaturation == null || kSaturation == undefined) - return; - - kSaturation = +kSaturation || 0.0; // coerce to number - - this.kSaturation = Math.max(-1, Math.min(kSaturation, 1)); - }; - - // @api - ColorCfg.prototype.getSaturation = function() { - return this.kSaturation; - }; - - // @api - ColorCfg.prototype.setGamma = function(gamma) { - if (gamma == null || gamma == undefined) - return; - - gamma = +gamma; // coerce to number - this.kGamma = Math.max(0.1, Math.min(gamma, 10)); - }; - - // @api - ColorCfg.prototype.getGamma = function() { - return this.kGamma; - }; - - // @api - ColorCfg.prototype.setOpacity = function(opacity) { - if (opacity == null || opacity == undefined) - return; - - opacity = +opacity; // coerce to number - this.opacity = Math.max(0, Math.min(opacity, 1)); - }; - - // @oldapi - ColorCfg.prototype.setAlpha = ColorCfg.prototype.setOpacity; - - // @api - ColorCfg.prototype.getOpacity = function() { - return this.opacity; - }; - - // @api - ColorCfg.prototype.getAlpha = ColorCfg.prototype.getOpacity; - - // @api - ColorCfg.prototype.setBlendingConfig = function(additive) { - if (additive === null || additive === undefined) - return; - - this.additiveBlending = additive; - }; - - ColorCfg.prototype.getBlendingConfig = function() { - return this.additiveBlending; - }; - - // @api - // Optional arguments, - ColorCfg.prototype.setColormap = function(colormap, options) { - /// colormap - this.colormap = (colormap && colormap.toLowerCase()) || this.colormap; - - /// stretch - let stretch = (options && options.stretch) || this.stretch || "linear"; - this.stretch = stretch.toLowerCase() - - /// reversed - if (options && options.reversed !== undefined) { - this.reversed = options.reversed; - } - } - - // @api - ColorCfg.prototype.getColormap = function() { - return this.colormap; - }; - - ColorCfg.prototype.getReversed = function() { - return this.reversed; - }; - - // Sets the cuts for the current image format - ColorCfg.prototype.setCuts = function(minCut, maxCut, imgFormat) { - imgFormat = imgFormat || this.imgFormat; - - if (minCut instanceof Object) { - // Mincut is given in the form of an javascript object with all the formats - this.minCut = {...this.minCut, ...minCut}; - } else if (minCut !== null && minCut !== undefined) { - this.minCut[imgFormat] = minCut; - } - - if (maxCut instanceof Object) { - this.maxCut = {...this.maxCut, ...maxCut}; - } else if (maxCut !== null && maxCut !== undefined) { - this.maxCut[imgFormat] = maxCut; - } - }; - - // Returns the cuts for the current image format - ColorCfg.prototype.getCuts = function() { - return [ - this.minCut[this.imgFormat], - this.maxCut[this.imgFormat] - ]; - }; - - return ColorCfg; - })(); diff --git a/src/js/DefaultHiPSList.js b/src/js/DefaultHiPSList.js index 0458e2e4..d6cb1bb1 100644 --- a/src/js/DefaultHiPSList.js +++ b/src/js/DefaultHiPSList.js @@ -20,6 +20,8 @@ // along with Aladin Lite. If not, see . // +import { DataproductType } from "../core/pkg/core"; + export let HiPSList = (function () { function HiPSList() {} @@ -33,6 +35,7 @@ export let HiPSList = (function () { imgFormat: "jpeg", cooFrame: "equatorial", startUrl: "https://alasky.cds.unistra.fr/DSS/DSSColor", + dataproductType: "image", }, { creatorDid: "ivo://erosita/dr1/rate/rgb", @@ -42,7 +45,8 @@ export let HiPSList = (function () { tileSize: 512, imgFormat: "png", cooFrame: "equatorial", - startUrl: "https://erosita.mpe.mpg.de/dr1/erodat/static/hips/eRASS1_RGB_Rate_c010/" + startUrl: "https://erosita.mpe.mpg.de/dr1/erodat/static/hips/eRASS1_RGB_Rate_c010/", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/2MASS/color", @@ -53,6 +57,7 @@ export let HiPSList = (function () { imgFormat: "jpeg", cooFrame: "equatorial", startUrl: "https://alaskybis.cds.unistra.fr/2MASS/Color", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/DSS2/red", @@ -64,12 +69,13 @@ export let HiPSList = (function () { cooFrame: "equatorial", numBitsPerPixel: 16, // options - minCut: 1000.0, - maxCut: 10000.0, + minCut: {fits: 1000.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 10000.0, jpeg: 255.0, png: 255.0}, colormap: "magma", stretch: "Linear", imgFormat: "fits", startUrl: "https://alaskybis.cds.unistra.fr/DSS/DSS2Merged", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/DM/I/350/gaiaedr3", @@ -79,12 +85,13 @@ export let HiPSList = (function () { tileSize: 512, numBitsPerPixel: -32, cooFrame: "equatorial", - minCut: 0, - maxCut: 12000, + minCut: {fits: 0.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 12000.0, jpeg: 255.0, png: 255.0}, stretch: "asinh", colormap: "rdylbu", imgFormat: "fits", startUrl: "https://alaskybis.cds.unistra.fr/ancillary/GaiaEDR3/density-map", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/PanSTARRS/DR1/g", @@ -96,11 +103,12 @@ export let HiPSList = (function () { cooFrame: "equatorial", numBitsPerPixel: -32, // options - minCut: -34, - maxCut: 7000, + minCut: {fits: -34.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 7000.0, jpeg: 255.0, png: 255.0}, stretch: "asinh", colormap: "redtemperature", - startUrl: "https://alasky.cds.unistra.fr/Pan-STARRS/DR1/g" + startUrl: "https://alasky.cds.unistra.fr/Pan-STARRS/DR1/g", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/PanSTARRS/DR1/color-z-zg-g", @@ -111,6 +119,7 @@ export let HiPSList = (function () { imgFormat: "jpeg", cooFrame: "equatorial", startUrl: "https://alasky.cds.unistra.fr/Pan-STARRS/DR1/color-z-zg-g", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/DECaPS/DR2/color", @@ -120,7 +129,8 @@ export let HiPSList = (function () { cooFrame: "equatorial", tileSize: 512, imgFormat: "png", - startUrl: "https://alasky.cds.unistra.fr/DECaPS/DR2/CDS_P_DECaPS_DR2_color" + startUrl: "https://alasky.cds.unistra.fr/DECaPS/DR2/CDS_P_DECaPS_DR2_color", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/Fermi/color", @@ -131,6 +141,7 @@ export let HiPSList = (function () { tileSize: 512, cooFrame: "equatorial", startUrl: "https://alasky.cds.unistra.fr/Fermi/Color", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/GALEXGR6_7/NUV", @@ -140,7 +151,8 @@ export let HiPSList = (function () { imgFormat: "png", tileSize: 512, cooFrame: "equatorial", - startUrl: "https://alasky.cds.unistra.fr/GALEX/GALEXGR6_7_NUV" + startUrl: "https://alasky.cds.unistra.fr/GALEX/GALEXGR6_7_NUV", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/IRIS/color", @@ -151,6 +163,7 @@ export let HiPSList = (function () { imgFormat: "jpeg", cooFrame: "galactic", startUrl: "https://alasky.cds.unistra.fr/IRISColor", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/Mellinger/color", @@ -160,7 +173,8 @@ export let HiPSList = (function () { tileSize: 512, imgFormat: "jpeg", cooFrame: "galactic", - startUrl: "https://alasky.cds.unistra.fr/MellingerRGB" + startUrl: "https://alasky.cds.unistra.fr/MellingerRGB", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/SDSS9/color", @@ -170,7 +184,8 @@ export let HiPSList = (function () { tileSize: 512, imgFormat: "jpeg", cooFrame: "equatorial", - startUrl: "https://alasky.cds.unistra.fr/SDSS/DR9/color" + startUrl: "https://alasky.cds.unistra.fr/SDSS/DR9/color", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/SPITZER/color", @@ -180,7 +195,8 @@ export let HiPSList = (function () { tileSize: 512, imgFormat: "jpeg", cooFrame: "galactic", - startUrl: "https://alasky.cds.unistra.fr/Spitzer/SpitzerI1I2I4color" + startUrl: "https://alasky.cds.unistra.fr/Spitzer/SpitzerI1I2I4color", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/allWISE/color", @@ -190,7 +206,8 @@ export let HiPSList = (function () { tileSize: 512, imgFormat: "jpeg", cooFrame: "equatorial", - startUrl: "https://alaskybis.cds.unistra.fr/AllWISE/RGB-W4-W2-W1" + startUrl: "https://alaskybis.cds.unistra.fr/AllWISE/RGB-W4-W2-W1", + dataproductType: "image", }, { creatorDid: "ivo://CDS/P/SDSS9/g", @@ -201,31 +218,34 @@ export let HiPSList = (function () { numBitsPerPixel: 16, imgFormat: "fits", cooFrame: "equatorial", - minCut: 0, - maxCut: 1.8, + minCut: {fits: 0.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 1.8, jpeg: 255.0, png: 255.0}, stretch: "linear", colormap: "redtemperature", - startUrl: "https://alasky.cds.unistra.fr/SDSS/DR9/band-g" + startUrl: "https://alasky.cds.unistra.fr/SDSS/DR9/band-g", + dataproductType: "image", }, { id: "P/Finkbeiner", name: "Halpha", maxOrder: 3, - minCut: -10, - maxCut: 800, + minCut: {fits: -10.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 800.0, jpeg: 255.0, png: 255.0}, colormap: "rdbu", imgFormat: "fits", - startUrl: "https://alasky.cds.unistra.fr/FinkbeinerHalpha" + startUrl: "https://alasky.cds.unistra.fr/FinkbeinerHalpha", + dataproductType: "image", }, { id: "P/VTSS/Ha", name: "VTSS-Ha", maxOrder: 3, - minCut: -10.0, - maxCut: 100.0, + minCut: {fits: -10.0, jpeg: 0.0, png: 0.0}, + maxCut: {fits: 100.0, jpeg: 255.0, png: 255.0}, colormap: "grayscale", imgFormat: "fits", - startUrl: "https://alasky.cds.unistra.fr/VTSS/Ha" + startUrl: "https://alasky.cds.unistra.fr/VTSS/Ha", + dataproductType: "image", }, { id: "P/GLIMPSE360", diff --git a/src/js/HiPS.js b/src/js/HiPS.js index 3d80f91f..2a237727 100644 --- a/src/js/HiPS.js +++ b/src/js/HiPS.js @@ -30,7 +30,6 @@ * *****************************************************************************/ import { ALEvent } from "./events/ALEvent.js"; -import { ColorCfg } from "./ColorCfg.js"; import { HiPSProperties } from "./HiPSProperties.js"; import { Aladin } from "./Aladin.js"; import { CooFrameEnum } from "./CooFrameEnum.js"; @@ -59,7 +58,7 @@ PropertyParser.cooFrame = function (properties) { let cooFrame = (properties && properties.hips_body && "ICRSd") || (properties && properties.hips_frame) || - "icrs"; + "ICRS"; return cooFrame; }; @@ -80,12 +79,12 @@ PropertyParser.minOrder = function (properties) { return minOrder; }; -PropertyParser.acceptedFormats = function (properties) { - let acceptedFormats = (properties && properties.hips_tile_format) || "jpeg"; +PropertyParser.formats = function (properties) { + let formats = properties?.hips_tile_format; - acceptedFormats = acceptedFormats.split(" ").map((fmt) => fmt.toLowerCase()); + formats = formats?.split(" ").map((fmt) => fmt.toLowerCase()); - return acceptedFormats; + return formats; }; PropertyParser.initialFov = function (properties) { @@ -177,7 +176,7 @@ PropertyParser.isPlanetaryBody = function (properties) { * @property {boolean} [reversed=false] - If true, the colormap is reversed; otherwise, it is not reversed. * @property {number} [minCut] - The minimum cut value for the color configuration. If not given, 0.0 for JPEG/PNG surveys, the value of the property file for FITS surveys * @property {number} [maxCut] - The maximum cut value for the color configuration. If not given, 1.0 for JPEG/PNG surveys, the value of the property file for FITS surveys - * @property {boolean} [additive=false] - If true, additive blending is applied; otherwise, it is not applied. + * @property {boolean} [blending=false] - If true, additive blending is applied; otherwise, it is not applied. * @property {number} [gamma=1.0] - The gamma correction value for the color configuration. * @property {number} [saturation=0.0] - The saturation value for the color configuration. * @property {number} [brightness=0.0] - The brightness value for the color configuration. @@ -256,7 +255,7 @@ export let HiPS = (function () { * @constructs HiPS * * @param {string} id - Mandatory unique identifier for the layer. Can be an arbitrary name - * @param {string|FileList|HiPSLocalFiles} url - Can be: + * @param {string|FileList|HiPSLocalFiles} location - Can be: *
    *
  • An http url towards a HiPS.
  • *
  • A relative path to your HiPS
  • @@ -273,11 +272,11 @@ export let HiPS = (function () { // Unique identifier for a survey this.id = id; - this.options = options; this.name = (options && options.name) || id; - this.startUrl = options.startUrl; + this.startUrl = options && options.startUrl; this.requestMode = options && options.requestMode || 'cors'; this.requestCredentials = options && options.requestCredentials || 'same-origin'; + this.type = "hips" this.slice = 0; @@ -318,21 +317,68 @@ export let HiPS = (function () { this.url = location; - this.maxOrder = options.maxOrder; - this.minOrder = options.minOrder || 0; - this.cooFrame = CooFrameEnum.fromString(options.cooFrame, null); - this.tileSize = options.tileSize; - this.skyFraction = options.skyFraction; - this.imgFormat = options.imgFormat; - this.acceptedFormats = options.formats; - this.defaultFitsMinCut = options.defaultFitsMinCut; - this.defaultFitsMaxCut = options.defaultFitsMaxCut; - this.numBitsPerPixel = options.numBitsPerPixel; - this.creatorDid = options.creatorDid; - this.errorCallback = options.errorCallback; - this.successCallback = options.successCallback; + this.maxOrder = options && options.maxOrder; + this.minOrder = (options && options.minOrder) || 0; + this.cooFrame = options && CooFrameEnum.fromString(options.cooFrame, null)?.system; + this.tileSize = options && options.tileSize; + this.skyFraction = options && options.skyFraction; + this.imgFormat = options && options.imgFormat; + this.formats = options && options.formats; + this.defaultFitsMinCut = options && options.defaultFitsMinCut; + this.defaultFitsMaxCut = options && options.defaultFitsMaxCut; + this.numBitsPerPixel = options && options.numBitsPerPixel; + this.creatorDid = options && options.creatorDid; + this.errorCallback = options && options.errorCallback; + this.successCallback = options && options.successCallback; + this.dataproductType = options && options.dataproductType; + this.tileDepth = options && options.tileDepth; + this.orderFreq = options && options.orderFreq; + this.emMin = options && options.emMin; + this.emMax = options && options.emMax; - this.colorCfg = new ColorCfg(options); + // Opacity of the survey/image + this.opacity = (options && options.opacity) || 1.0; + + // Colormap config options + this.colormap = (options && options.colormap) || "native"; + this.colormap = this.colormap.toLowerCase(); + + this.stretch = (options && options.stretch) || "linear"; + this.stretch = this.stretch.toLowerCase(); + this.reversed = false; + // Keep the image tile format because we want the cuts + this.imgFormat = (options && options.imgFormat) || 'png'; + + if (options && options.reversed === true) { + this.reversed = true; + } + + this.minCut = { + webp: 0.0, + jpeg: 0.0, + png: 0.0, + fits: undefined // wait the default value coming from the properties + }; + + this.maxCut = { + webp: 255.0, + jpeg: 255.0, + png: 255.0, + fits: undefined // wait the default value coming from the properties + }; + + this.setCuts(options.minCut, options.maxCut); + + this.blending = options && options.blending; + if (this.blending === undefined) { + this.blending = false; + } + + // A default value for gamma correction + this.gamma = (options && options.gamma) || 1.0; + this.saturation = (options && options.saturation) || 0.0; + this.brightness = (options && options.brightness) || 0.0; + this.contrast = (options && options.contrast) || 0.0; let self = this; @@ -441,13 +487,12 @@ export let HiPS = (function () { hips_order: self.maxOrder, hips_service_url: self.url, hips_tile_width: self.tileSize, - hips_frame: self.cooFrame.label + hips_frame: self.cooFrame }) } if (self.updateHiPSCache) { - self._saveInCache(); - self.updateHiPSCache = false; + self._updateMetadata() } resolve(self); @@ -491,8 +536,8 @@ export let HiPS = (function () { // HiPS Cube special keywords self.cubeDepth = properties && properties.hips_cube_depth && +properties.hips_cube_depth; self.cubeFirstFrame = properties && properties.hips_cube_firstframe && +properties.hips_cube_firstframe; - self.emMin = properties && properties.em_min && +properties.em_min; - self.emMax = properties && properties.em_max && +properties.em_max; + self.emMin = (properties && properties.em_min && +properties.em_min) || self.emMin; + self.emMax = (properties && properties.em_max && +properties.em_max) || self.emMax; if (self.emMax < self.emMin) { let tmp = self.emMin; @@ -500,11 +545,11 @@ export let HiPS = (function () { self.emMax = tmp; } - self.hipsDataMinMax = PropertyParser.hipsDataMinmax(properties); + self.dataMinMax = PropertyParser.hipsDataMinmax(properties); // HiPS3D special keywords - self.hipsOrderFreq = properties && properties.hips_order_freq && +properties.hips_order_freq; - self.hipsTileDepth = properties && properties.hips_tile_depth && +properties.hips_tile_depth; + self.orderFreq = (properties && properties.hips_order_freq && +properties.hips_order_freq) || self.orderFreq; + self.tileDepth = (properties && properties.hips_tile_depth && +properties.hips_tile_depth) || self.tileDepth; self.obsRestFreq = properties && properties.obs_restfreq && +properties.obs_restfreq; // Max order @@ -514,15 +559,15 @@ export let HiPS = (function () { } // dataproduct type - self.dataproductType = properties && properties.dataproduct_type; + self.dataproductType = properties && properties.dataproduct_type || self.dataproductType; // Tile size self.tileSize = PropertyParser.tileSize(properties) || self.tileSize; // Tile formats - self.acceptedFormats = - PropertyParser.acceptedFormats(properties) || self.acceptedFormats; + self.formats = + PropertyParser.formats(properties) || self.formats || ["jpeg"]; // Min order const minOrder = PropertyParser.minOrder(properties) @@ -535,7 +580,7 @@ export let HiPS = (function () { PropertyParser.cooFrame(properties); // Parse the cooframe from the properties but if it fails, take the one given by the user // If the user gave nothing, then take ICRS as the default one - self.cooFrame = CooFrameEnum.fromString(cooFrame, self.cooFrame || CooFrameEnum.ICRS); + self.cooFrame = CooFrameEnum.fromString(cooFrame, self.cooFrame || CooFrameEnum.ICRS).system; // sky fraction self.skyFraction = PropertyParser.skyFraction(properties); @@ -576,21 +621,21 @@ export let HiPS = (function () { self.creatorDid = self.creatorDid || self.id || self.url; // check the imgFormat with respect to the formats accepted image format - const chooseTileFormat = (acceptedFormats) => { - if (acceptedFormats.indexOf("webp") >= 0) { + const chooseTileFormat = (formats) => { + if (formats.indexOf("webp") >= 0) { return "webp"; - } else if (acceptedFormats.indexOf("png") >= 0) { + } else if (formats.indexOf("png") >= 0) { return "png"; - } else if (acceptedFormats.indexOf("jpeg") >= 0) { + } else if (formats.indexOf("jpeg") >= 0) { return "jpeg"; - } else if (acceptedFormats.indexOf("fits") >= 0) { + } else if (formats.indexOf("fits") >= 0) { return "fits"; - } else if (acceptedFormats.indexOf("fits.fz") >= 0) { + } else if (formats.indexOf("fits.fz") >= 0) { return "fits"; } else { throw ( "Unsupported format(s) found in the properties: " + - acceptedFormats + formats ); } }; @@ -598,9 +643,9 @@ export let HiPS = (function () { // Set an image format with respect to the ones available for that HiPS if: // * the format is unknown // * the format is known but is not available for that HiPS - if (!self.imgFormat || !self.acceptedFormats.includes(self.imgFormat)) { + if (!self.imgFormat || !self.formats.includes(self.imgFormat)) { // Switch automatically to a available format - let imgFormat = chooseTileFormat(self.acceptedFormats); + let imgFormat = chooseTileFormat(self.formats); self.setImageFormat(imgFormat) console.info(self.id + " tile format chosen: " + self.imgFormat) @@ -649,18 +694,7 @@ export let HiPS = (function () { * @returns {string[]} Returns the formats accepted for the survey, i.e. the formats of tiles that are availables. Could be PNG, WEBP, JPG and FITS. */ HiPS.prototype.getAvailableFormats = function () { - return this.acceptedFormats; - }; - - /** - * Sets the opacity factor when rendering the HiPS - * - * @memberof HiPS - * - * @param {number} opacity - Opacity of the survey to set. Between 0 and 1 - */ - HiPS.prototype.setOpacity = function (opacity) { - this.setOptions({opacity}) + return this.formats; }; /** @@ -668,7 +702,7 @@ export let HiPS = (function () { * * @memberof HiPS * - * @param {boolean} [additive=false] - When rendering this survey on top of the already rendered ones, the final color of the screen is computed like: + * @param {boolean} [blending=false] - When rendering this survey on top of the already rendered ones, the final color of the screen is computed like: *
    *
    opacity * this_survey_color + (1 - opacity) * already_rendered_color for the default mode *
    opacity * this_survey_color + already_rendered_color for the additive mode @@ -677,12 +711,12 @@ export let HiPS = (function () { * Additive mode allows you to do linear survey color combination i.e. let's define 3 surveys named s1, s2, s3. Each could be associated to one color channel, i.e. s1 with red, s2 with green and s3 with the blue color channel. * If the additive blending mode is enabled, then the final pixel color of your screen will be: rgb = [s1_opacity * s1_color; s2_opacity * s2_color; s3_opacity * s3_color] */ - HiPS.prototype.setBlendingConfig = function (additive = false) { - this.setOptions({additive}); + HiPS.prototype.setBlendingConfig = function (blending = false) { + this.setOptions({blending}); }; HiPS.prototype.isSpectralCube = function() { - return this.hipsTileDepth !== undefined && this.hipsTileDepth !== null; + return this.tileDepth !== undefined && this.tileDepth !== null; } /** @@ -721,9 +755,7 @@ export let HiPS = (function () { * @param {boolean} [options.reversed=false] - Reverse the colormap axis. */ HiPS.prototype.setColormap = function (colormap, options) { - colormap = colormap || this.options.colormap; - - this.setOptions({colormap, ...options}) + this.setOptions({colormap, ...options}); }; /** @@ -737,54 +769,11 @@ export let HiPS = (function () { * @param {number} maxCut - The high cut value to set for the HiPS. * @param {string} [imgFormat] - The image format for which one wants to set the cuts. By default, the format used is the current imageFormat */ - HiPS.prototype.setCuts = function (minCut, maxCut, imgFormat) { - imgFormat = imgFormat?.toLowerCase(); - - if (imgFormat === "jpg") { - imgFormat = "jpeg"; - } - - this.setOptions({minCut, maxCut, cutFormat: imgFormat}) + HiPS.prototype.setCuts = function (minCut, maxCut, cutFormat) { + this.setOptions({minCut, maxCut, cutFormat}) }; - /** - * Returns the low and high cuts under the form of a 2 element array - * - * @memberof HiPS - * - * @returns {number[]} The low and high cut values for the HiPS. - */ - HiPS.prototype.getCuts = function () { - return this.colorCfg.getCuts(); - }; - - /** - * Sets the gamma correction factor for the HiPS. - * - * This method updates the gamma of the HiPS. - * - * @memberof HiPS - * - * @param {number} gamma - The saturation value to set for the HiPS. Between 0.1 and 10 - */ - HiPS.prototype.setGamma = function (gamma) { - this.setOptions({gamma}) - }; - - /** - * Sets the saturation for the HiPS. - * - * This method updates the saturation of the HiPS. - * - * @memberof HiPS - * - * @param {number} saturation - The saturation value to set for the HiPS. Between 0 and 1 - */ - HiPS.prototype.setSaturation = function (saturation) { - this.setOptions({saturation}) - }; - - /** + /** * Sets the brightness for the HiPS. * * This method updates the brightness of the HiPS. @@ -793,10 +782,15 @@ export let HiPS = (function () { * * @param {number} brightness - The brightness value to set for the HiPS. Between 0 and 1 */ - HiPS.prototype.setBrightness = function (brightness) { + HiPS.prototype.setBrightness = function(brightness) { this.setOptions({brightness}) }; + // @api + HiPS.prototype.getBrightness = function() { + return this.brightness; + }; + /** * Sets the contrast for the HiPS. * @@ -806,10 +800,103 @@ export let HiPS = (function () { * * @param {number} contrast - The contrast value to set for the HiPS. Between 0 and 1 */ - HiPS.prototype.setContrast = function (contrast) { + HiPS.prototype.setContrast = function(contrast) { this.setOptions({contrast}) }; + // @api + HiPS.prototype.getContrast = function() { + return this.kContrast; + }; + + /** + * Sets the saturation for the HiPS. + * + * This method updates the saturation of the HiPS. + * + * @memberof HiPS + * + * @param {number} saturation - The saturation value to set for the HiPS. Between 0 and 1 + */ + HiPS.prototype.setSaturation = function(saturation) { + this.setOptions({saturation}) + }; + + // @api + HiPS.prototype.getSaturation = function() { + return this.kSaturation; + }; + + /** + * Sets the gamma correction factor for the HiPS. + * + * This method updates the gamma of the HiPS. + * + * @memberof HiPS + * + * @param {number} gamma - The saturation value to set for the HiPS. Between 0.1 and 10 + */ + HiPS.prototype.setGamma = function(gamma) { + this.setOptions({gamma}) + }; + + // @api + HiPS.prototype.getGamma = function() { + return this.kGamma; + }; + + /** + * Sets the opacity factor when rendering the HiPS + * + * @memberof HiPS + * + * @param {number} opacity - Opacity of the survey to set. Between 0 and 1 + */ + HiPS.prototype.setOpacity = function(opacity) { + this.setOptions({opacity}) + }; + + /** + * Get the opacity of the HiPS layer + * + * @memberof HiPS + * + * @returns {number} The opacity of the layer + */ + HiPS.prototype.getOpacity = function() { + return this.opacity; + }; + + // @api + HiPS.prototype.getAlpha = HiPS.prototype.getOpacity; + + HiPS.prototype.getBlendingConfig = function() { + return this.blending; + }; + + // @api + HiPS.prototype.getColormap = function() { + return this.colormap; + }; + + HiPS.prototype.getReversed = function() { + return this.reversed; + }; + + /** + * Returns the low and high cuts under the form of a 2 element array + * + * @memberof HiPS + * + * @returns {number[]} The low and high cut values for the HiPS. + */ + HiPS.prototype.getCuts = function() { + return [ + this.minCut[this.imgFormat], + this.maxCut[this.imgFormat] + ]; + }; + HiPS.prototype.setSliceNumber = function(slice) { this.slice = slice; @@ -830,6 +917,9 @@ export let HiPS = (function () { * @param {number} [options.value] = The frequency value expressed in `options.unit` * @param {"Hz"|"m"|"m/s"} [options.unit="Hz"] - The unit of the frequency passed * @param {number} [options.restFreq] - "The rest frequency (in Hz) to use for computing the velocity in m.s-1" + * + * @example + * hips3d.setFrequency({ value: 1420302592, unit: 'Hz' }) */ HiPS.prototype.setFrequency = function(options) { if (this.added) { @@ -858,12 +948,22 @@ export let HiPS = (function () { } } + /** + * Get the frequency in Hz + * + * @memberof HiPS + */ HiPS.prototype.getFrequency = function() { if (this.added) { return this.view.wasm.getFreq(this.layer); } } + /** + * Get the frequency window around the current observed frequency in Hz + * + * @memberof HiPS + */ HiPS.prototype.getFrequencyWindow = function() { if (this.added) { return this.view.wasm.getFreqWindow(this.layer); @@ -874,10 +974,7 @@ export let HiPS = (function () { HiPS.prototype._updateMetadata = function () { try { if (this.added) { - this.view.wasm.setImageMetadata(this.layer, { - ...this.colorCfg.get(), - imgFormat: this.imgFormat, - }); + this.view.wasm.setImageMetadata(this.layer, this._prepareMetadataForWASM()); // once the meta have been well parsed, we can set the meta ALEvent.LAYER_CHANGED.dispatchedTo(this.view.aladinDiv, { layer: this, @@ -906,14 +1003,93 @@ export let HiPS = (function () { * @param {boolean} [options.reversed=false] - If true, the colormap is reversed; otherwise, it is not reversed. * @param {number} [options.minCut] - The minimum cut value for the color configuration. If not given, 0.0 for JPEG/PNG surveys, the value of the property file for FITS surveys * @param {number} [options.maxCut] - The maximum cut value for the color configuration. If not given, 1.0 for JPEG/PNG surveys, the value of the property file for FITS surveys - * @param {boolean} [options.additive=false] - If true, additive blending is applied; otherwise, it is not applied. + * @param {boolean} [options.blending=false] - If true, additive blending is applied; otherwise, it is not applied. * @param {number} [options.gamma=1.0] - The gamma correction value for the color configuration. * @param {number} [options.saturation=0.0] - The saturation value for the color configuration. * @param {number} [options.brightness=0.0] - The brightness value for the color configuration. * @param {number} [options.contrast=0.0] - The contrast value for the color configuration. */ HiPS.prototype.setOptions = function(options) { - this.colorCfg.setOptions(options); + /// imgFormat + if (options && options.imgFormat) { + this.imgFormat = options.imgFormat + } + + /// colormap + if (options && options.colormap) { + this.colormap = options.colormap.toLowerCase() + } + + /// stretch + if (options && options.stretch) { + let stretch = options.stretch; + this.stretch = stretch.toLowerCase() + } + + /// reversed + if (options && options.reversed !== undefined) { + this.reversed = options.reversed; + } + + /// cuts + let cutFormat = options.cutFormat?.toLowerCase() || this.imgFormat; + + if (cutFormat === "jpg") { + cutFormat = "jpeg"; + } + + + let minCut = options && options.minCut; + if (minCut instanceof Object) { + // Mincut is given in the form of an javascript object with all the formats + this.minCut = {...this.minCut, ...minCut}; + } else if (minCut !== null && minCut !== undefined) { + this.minCut[cutFormat] = minCut; + } + + let maxCut = options && options.maxCut; + if (maxCut instanceof Object) { + this.maxCut = {...this.maxCut, ...maxCut}; + } else if (maxCut !== null && maxCut !== undefined) { + this.maxCut[cutFormat] = maxCut; + } + + if (options && Utils.isNumber(options.brightness)) { + let brightness = options.brightness; + + brightness = +brightness || 0.0; // coerce to number + this.brightness = Math.max(-1, Math.min(brightness, 1)); + } + + if (options && Utils.isNumber(options.saturation)) { + let saturation = options.saturation; + saturation = +saturation || 0.0; // coerce to number + + this.saturation = Math.max(-1, Math.min(saturation, 1)); + } + + if (options && Utils.isNumber(options.contrast)) { + let contrast = options.contrast; + + contrast = +contrast || 0.0; // coerce to number + this.contrast = Math.max(-1, Math.min(contrast, 1)); + } + + if (options && Utils.isNumber(options.gamma)) { + let gamma = options.gamma; + gamma = +gamma; // coerce to number + this.gamma = Math.max(0.1, Math.min(gamma, 10)); + } + + if (options && Utils.isNumber(options.opacity)) { + let opacity = options.opacity; + opacity = +opacity; // coerce to number + this.opacity = Math.max(0, Math.min(opacity, 1)); + } + + if (options && options.blending) { + this.blending = options.blending; + } /// Set image format if (options.imgFormat) { @@ -932,10 +1108,10 @@ export let HiPS = (function () { } else { // Passed the check, we erase the image format with the new one // We do nothing if the imgFormat is the same - + // Check the properties to see if the given format is available among the list // If the properties have not been retrieved yet, it will be tested afterwards - const availableFormats = this.acceptedFormats; + const availableFormats = this.formats; // user wants a fits but the metadata tells this format is not available if (!availableFormats || (availableFormats && availableFormats.indexOf(imgFormat) >= 0)) { this.imgFormat = imgFormat; @@ -951,13 +1127,6 @@ export let HiPS = (function () { } } - this.options = { - ...this.options, - ...options, - minCut: this.colorCfg.minCut, - maxCut: this.colorCfg.maxCut - }; - this._updateMetadata(); }; @@ -984,24 +1153,6 @@ export let HiPS = (function () { */ HiPS.prototype.setAlpha = HiPS.prototype.setOpacity; - // @api - HiPS.prototype.getColorCfg = function () { - return this.colorCfg; - }; - - /** - * Get the opacity of the HiPS layer - * - * @memberof HiPS - * - * @returns {number} The opacity of the layer - */ - HiPS.prototype.getOpacity = function () { - return this.colorCfg.getOpacity(); - }; - - HiPS.prototype.getAlpha = HiPS.prototype.getOpacity; - /** * Probe the HiPS at a screen pixel location. * @@ -1076,11 +1227,43 @@ export let HiPS = (function () { return; } + this.updateHiPSCache = false; + let self = this; let hipsCache = this.view.aladin.hipsCache; if (hipsCache.contains(self.id)) { - hipsCache.append(self.id, this.options) + hipsCache.update(self.id, { + creatorDid: self.creatorDid, + url: self.url, + maxOrder: self.maxOrder, + cooFrame: self.cooFrame, + tileSize: self.tileSize, + formats: self.formats, + numBitsPerPixel: self.numBitsPerPixel, + skyFraction: self.skyFraction, + minOrder: self.minOrder, + initialFov: self.initialFov, + initialRa: self.initialRa, + initialDec: self.initialDec, + emMin: self.emMin, + emMax: self.emMax, + // HiPS Cube + cubeDepth: self.cubeDepth, + // HiPS3D + tileDepth: self.tileDepth, + orderFreq: self.orderFreq, + // Dataproduct type + dataproductType: self.dataproductType, + isPlanetaryBody: self.isPlanetaryBody(), + hipsBody: self.hipsBody, + requestCredentials: self.requestCredentials, + requestMode: self.requestMode, + name: this.name, + id: this.id, + type: this.type, + ...this._getMetadata(), + }) } }; @@ -1093,6 +1276,25 @@ export let HiPS = (function () { } }; + HiPS.prototype._getMetadata = function() { + return { + imgFormat: this.imgFormat, + blending: this.blending, + opacity: this.opacity, + // Tonal corrections constants + gamma: this.gamma, + saturation: this.saturation, + brightness: this.brightness, + contrast: this.contrast, + + stretch: this.stretch, + minCut: this.minCut, + maxCut: this.maxCut, + reversed: this.reversed, + colormap: this.colormap, + }; + }; + HiPS.prototype._addToView = function (layer) { if (!this.view) return this; @@ -1106,22 +1308,22 @@ export let HiPS = (function () { creatorDid: self.creatorDid, url: self.url, maxOrder: self.maxOrder, - cooFrame: self.cooFrame.system, + cooFrame: self.cooFrame, tileSize: self.tileSize, - formats: self.acceptedFormats, + formats: self.formats, bitpix: self.numBitsPerPixel, skyFraction: self.skyFraction, minOrder: self.minOrder, - hipsInitialFov: self.initialFov, - hipsInitialRa: self.initialRa, - hipsInitialDec: self.initialDec, + initialFov: self.initialFov, + initialRa: self.initialRa, + initialDec: self.initialDec, emMin: self.emMin, emMax: self.emMax, // HiPS Cube - hipsCubeDepth: self.cubeDepth, + cubeDepth: self.cubeDepth, // HiPS3D - hipsTileDepth: self.hipsTileDepth, - hipsOrderFreq: self.hipsOrderFreq, + tileDepth: self.tileDepth, + orderFreq: self.orderFreq, // Dataproduct type dataproductType: self.dataproductType, isPlanetaryBody: self.isPlanetaryBody(), @@ -1129,10 +1331,7 @@ export let HiPS = (function () { requestCredentials: self.requestCredentials, requestMode: self.requestMode, }, - meta: { - ...this.colorCfg.get(), - imgFormat: this.imgFormat, - } + meta: this._prepareMetadataForWASM() }; let localFiles; @@ -1175,7 +1374,41 @@ export let HiPS = (function () { return this }; + HiPS.prototype._prepareMetadataForWASM = function() { + let metadata = this._getMetadata(); + let blending = { + srcColorFactor: 'SrcAlpha', + dstColorFactor: 'OneMinusSrcAlpha', + func: 'FuncAdd' + }; + + if (this.blending) { + blending = { + srcColorFactor: 'SrcAlpha', + dstColorFactor: 'One', + func: 'FuncAdd' + } + } + + let minCut = this.minCut[this.imgFormat] + if (this.imgFormat !== "fits") { + minCut /= 255.0 + } + + let maxCut = this.maxCut[this.imgFormat] + if (this.imgFormat !== "fits") { + maxCut /= 255.0 + } + + metadata["minCut"] = minCut; + metadata["maxCut"] = maxCut; + metadata["blending"] = blending; + + return metadata; + }; + HiPS.DEFAULT_SURVEY_ID = "P/DSS2/color"; return HiPS; })(); + diff --git a/src/js/HiPSCache.js b/src/js/HiPSCache.js index 90e4fea7..cedd5900 100644 --- a/src/js/HiPSCache.js +++ b/src/js/HiPSCache.js @@ -37,11 +37,17 @@ export let HiPSCache = (function () { this.cache = {} }; + HiPSCache.prototype.update = function (key, obj) { + this.cache[key] = obj; + + ALEvent.HIPS_CACHE_UPDATED.dispatchedTo(document.body); + }; + /* * key can be a CDS ID or an url. TODO could be an options.name too. */ - HiPSCache.prototype.append = function (key, options) { - this.cache[key] = options; + HiPSCache.prototype.append = function (key, obj) { + this.cache[key] = obj; ALEvent.HIPS_CACHE_UPDATED.dispatchedTo(document.body); }; @@ -59,7 +65,9 @@ export let HiPSCache = (function () { * key can be a CDS ID or an url. TODO could be an options.name too. */ HiPSCache.prototype.get = function (key) { - return this.cache[key]; + let obj = this.cache[key]; + + return obj; }; /* diff --git a/src/js/Image.js b/src/js/Image.js index 93055fd0..a57c7a38 100644 --- a/src/js/Image.js +++ b/src/js/Image.js @@ -29,11 +29,10 @@ * Authors: Matthieu Baumann [CDS] * *****************************************************************************/ -import { ColorCfg } from "./ColorCfg.js"; -import { Aladin } from "./Aladin.js"; import { Utils } from "./Utils"; import { AVM } from "./libs/avm.js"; import { HiPS } from "./HiPS.js"; +import { JSONP_PROXY } from "./Constants"; /** * @typedef {Object} WCS @@ -140,13 +139,55 @@ export let Image = (function () { this.id = url; this.name = (options && options.name) || this.url; this.imgFormat = options && options.imgFormat; - this.acceptedFormats = [this.imgFormat]; + this.formats = [this.imgFormat]; + this.type = "image" + + // Opacity of the survey/image + this.opacity = (options && options.opacity) || 1.0; + + // Colormap config options + this.colormap = (options && options.colormap) || "native"; + this.colormap = this.colormap.toLowerCase(); + + this.stretch = (options && options.stretch) || "linear"; + this.stretch = this.stretch.toLowerCase(); + this.reversed = false; + + if (options && options.reversed === true) { + this.reversed = true; + } + + this.minCut = { + webp: 0.0, + jpeg: 0.0, + png: 0.0, + fits: undefined // wait the default value coming from the properties + }; + + this.maxCut = { + webp: 255.0, + jpeg: 255.0, + png: 255.0, + fits: undefined // wait the default value coming from the properties + }; + + this.setCuts(options.minCut, options.maxCut); + + this.blending = options && options.blending; + if (this.blending === undefined) { + this.blending = false; + } + + // A default value for gamma correction + this.gamma = (options && options.gamma) || 1.0; + this.saturation = (options && options.saturation) || 0.0; + this.brightness = (options && options.brightness) || 0.0; + this.contrast = (options && options.contrast) || 0.0; // callbacks this.successCallback = options && options.successCallback; this.errorCallback = options && options.errorCallback; - this.colorCfg = new ColorCfg(options); this.options = options || {}; let self = this; @@ -301,8 +342,6 @@ export let Image = (function () { */ Image.prototype.setAlpha = HiPS.prototype.setOpacity; - Image.prototype.getColorCfg = HiPS.prototype.getColorCfg; - /** * Get the opacity of the image layer * @@ -341,7 +380,13 @@ export let Image = (function () { */ Image.prototype.getAvailableFormats = HiPS.prototype.getAvailableFormats; - + Image.prototype.getColormap = HiPS.prototype.getColormap; + Image.prototype.getReversed = HiPS.prototype.getReversed; + Image.prototype.getBrightness = HiPS.prototype.getBrightness; + Image.prototype.getSaturation = HiPS.prototype.getSaturation; + Image.prototype.getContrast = HiPS.prototype.getContrast; + Image.prototype.getGamma = HiPS.prototype.getGamma; + Image.prototype._setView = function (view) { this.view = view; }; @@ -361,7 +406,29 @@ export let Image = (function () { }; /* Private method view is already attached */ - Image.prototype._saveInCache = HiPS.prototype._saveInCache; + /* Precondition: view is attached */ + Image.prototype._saveInCache = function () { + if (!this.view) { + this.updateHiPSCache = true; + return; + } + + this.updateHiPSCache = false; + + let self = this; + let hipsCache = this.view.aladin.hipsCache; + + if (hipsCache.contains(self.id)) { + hipsCache.update(self.id, { + url: self.url, + formats: self.formats, + name: this.name, + id: this.id, + type: this.type, + ...this._getMetadata(), + }) + } + }; // Private method for updating the view with the new meta Image.prototype._updateMetadata = HiPS.prototype._updateMetadata; @@ -401,7 +468,7 @@ export let Image = (function () { } promise = promise.then((imageParams) => { - self.acceptedFormats = [self.imgFormat]; + self.formats = [self.imgFormat]; // There is at least one entry in imageParams self.added = true; @@ -451,31 +518,27 @@ export let Image = (function () { url: this.url, dataType: 'arrayBuffer', success: (buf) => { + self.imgFormat = 'fits'; return self.view.wasm.addFITSImage( new Uint8Array(buf), - { - ...self.colorCfg.get(), - imgFormat: 'fits', - }, + self._prepareMetadataForWASM(), layer ) }, error: (e) => { console.error(e) - console.info("Trying querying the FITS through proxy:" + Aladin.JSONP_PROXY) + console.info("Trying querying the FITS through proxy:" + JSONP_PROXY) // try as cors - const url = Aladin.JSONP_PROXY + '?url=' + self.url; + const url = JSONP_PROXY + '?url=' + self.url; return Utils.fetch({ - url: url, + url, dataType: 'arrayBuffer', success: (buf) => { + self.imgFormat = 'fits'; return self.view.wasm.addFITSImage( new Uint8Array(buf), - { - ...self.colorCfg.get(), - imgFormat: 'fits', - }, + self._prepareMetadataForWASM(), layer ) }, @@ -484,17 +547,33 @@ export let Image = (function () { }) .then((imageParams) => { self.imgFormat = 'fits' - self.colorCfg.setOptions({imgFormat: 'fits'}); - return Promise.resolve(imageParams); }) }; + Image.prototype._getMetadata = HiPS.prototype._getMetadata; + + Image.prototype._prepareMetadataForWASM = HiPS.prototype._prepareMetadataForWASM; + Image.prototype._addJPGOrPNG = function(layer) { let self = this; let img = document.createElement('img'); - return new Promise((resolve, reject) => { + console.log(this.url) + let checkImgFormat = Utils.fetchWithProxy({url: this.url, dataType: 'arrayBuffer'}) + .then(arrayBuffer => { + const view = new DataView(arrayBuffer); + // See the magic bytes for JPEG or PNG files + if (view.getUint8(0) === 0xFF && view.getUint8(1) === 0xD8) { + return "jpeg"; + } else if (view.getUint8(0) === 0x89 && view.getUint8(1) === 0x50) { + return "png"; + } else { + throw new Error("Unknown image format"); + } + }) + + let loadImg = new Promise((resolve, reject) => { img.src = this.url; img.crossOrigin = "Anonymous"; img.onload = () => { @@ -504,6 +583,8 @@ export let Image = (function () { canvas.width = img.width; canvas.height = img.height; + img.arrat + // Copy the image contents to the canvas var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, img.width, img.height); @@ -525,12 +606,20 @@ export let Image = (function () { // obj.tags (object) = An array containing all the loaded tags e.g. obj.tags['Headline'] // obj.wcs (object) = The wcs parsed from the image if (obj.wcsdata) { + if (obj.wcs.NAXIS1 === undefined) { + obj.wcs.NAXIS1 = img.width; + } + if (img.width !== obj.wcs.NAXIS1) { obj.wcs.CDELT1 = obj.wcs.CDELT1 * (obj.wcs.NAXIS1 / img.width); obj.wcs.CRPIX1 *= img.width / obj.wcs.NAXIS1; obj.wcs.NAXIS1 = img.width; } + if (obj.wcs.NAXIS2 === undefined) { + obj.wcs.NAXIS2 = img.height; + } + if (img.height !== obj.wcs.NAXIS2) { obj.wcs.CDELT2 = obj.wcs.CDELT2 * (obj.wcs.NAXIS2 / img.height); obj.wcs.CRPIX2 *= img.height / obj.wcs.NAXIS2; @@ -561,38 +650,32 @@ export let Image = (function () { return; } - console.error(e); - console.info("Using proxy", Aladin.JSONP_PROXY) + console.info("Using proxy", JSONP_PROXY) proxyUsed = true; - img.src = Aladin.JSONP_PROXY + '?url=' + self.url; + img.src = JSONP_PROXY + '?url=' + self.url; } - }) - .then((bytes) => { - let wcs = self.options && self.options.wcs; - wcs.NAXIS1 = wcs.NAXIS1 || img.width; - wcs.NAXIS2 = wcs.NAXIS2 || img.height; - return self.view.wasm - .addRGBAImage( - bytes, - wcs, - { - ...self.colorCfg.get(), - imgFormat: 'jpeg', - }, - layer - ) - }) - .then((imageParams) => { - self.imgFormat = 'jpeg' - self.colorCfg.setOptions({imgFormat: 'jpeg'}); - return Promise.resolve(imageParams); - }) - /*.catch((e) => { - console.error(e) - })*/ - .finally(() => { - img.remove(); }); + + return Promise.all([loadImg, checkImgFormat]) + .then(([imgBytes, imgFormat]) => { + let wcs = self.options && self.options.wcs; + wcs.NAXIS1 = wcs.NAXIS1 || img.width; + wcs.NAXIS2 = wcs.NAXIS2 || img.height; + wcs.NAXIS = wcs.NAXIS || 2; + + self.imgFormat = imgFormat; + + return self.view.wasm + .addRGBAImage( + imgBytes, + wcs, + self._prepareMetadataForWASM(), + layer + ) + }) + .finally(() => { + img.remove(); + }); }; return Image; diff --git a/src/js/MocServer.js b/src/js/MocServer.js index 227e67d6..0e7b32c1 100644 --- a/src/js/MocServer.js +++ b/src/js/MocServer.js @@ -50,7 +50,7 @@ export class MocServer { static getAllHiPSes() { if (!this._allHiPSes) { const params = { - expr: "dataproduct_type=image||dataproduct_type=cube", + expr: "dataproduct_type=image||dataproduct_type=spectral-cube", //expr: "dataproduct_type=image", get: "record", fmt: "json", @@ -69,8 +69,8 @@ export class MocServer { static getAllHiPSesInsideView(aladin) { let params = { - //expr: "dataproduct_type=image||dataproduct_type=cube", - expr: "dataproduct_type=image", + expr: "dataproduct_type=image||dataproduct_type=spectral-cube", + //expr: "dataproduct_type=image", get: "record", fmt: "json", fields: "ID", @@ -118,6 +118,7 @@ export class MocServer { }; this._allCatalogHiPSes = Utils.loadFromUrls(MocServer.MIRRORS_HTTPS, {data: params, dataType: 'json'}) + this._allCatalogHiPSes.then((aa) => console.log(aa)) } return this._allCatalogHiPSes; diff --git a/src/js/ProgressiveCat.js b/src/js/ProgressiveCat.js index 42c6cfe1..d301c7bf 100644 --- a/src/js/ProgressiveCat.js +++ b/src/js/ProgressiveCat.js @@ -248,18 +248,28 @@ export let ProgressiveCat = (function() { _loadMetadata: function() { var self = this; - let request = new Request(self.rootUrl + '/' + 'Metadata.xml', { + let request = new Request(self.rootUrl + '/' + 'metadata.xml', { method: 'GET' }) fetch(request) - .then((resp) => resp.text()) + .then((resp) => { + if (!resp.ok) { + let request = new Request(self.rootUrl + '/' + 'Metadata.xml', { + method: 'GET' + }) + return fetch(request) + .then((resp) => resp.text()) + } else { + return resp.text() + } + }) .then((text) => { let xml = ProgressiveCat.parser.parseFromString(text, "text/xml") self.fields = getFields(self, xml); self._loadAllskyNewMethod(); }) - .catch(err => self._loadAllskyOldMethod()); + .catch(err => self._loadAllskyOldMethod()) }, _loadAllskyNewMethod: function() { @@ -598,10 +608,6 @@ export let ProgressiveCat = (function() { (function(self, norder, ipix) { // wrapping function is needed to be able to retrieve norder and ipix in ajax success function var key = norder + '-' + ipix; Utils.fetch({ - /* - url: Aladin.JSONP_PROXY, - data: {"url": self.getTileURL(norder, ipix)}, - */ // ATTENTION : je passe en JSON direct, car je n'arrive pas a choper les 404 en JSONP url: self.getTileURL(norder, ipix), desc: "Get tile .tsv " + norder + ' ' + ipix + ' of ' + self.name, diff --git a/src/js/Reticle.js b/src/js/Reticle.js index 431a9657..cbf08d62 100644 --- a/src/js/Reticle.js +++ b/src/js/Reticle.js @@ -54,7 +54,7 @@ export let Reticle = (function() { let color = options && options.reticleColor || Aladin.DEFAULT_OPTIONS.reticleColor; let size = options && options.reticleSize || Aladin.DEFAULT_OPTIONS.reticleSize; - + let show; if (options.showReticle === undefined) { show = Aladin.DEFAULT_OPTIONS.showReticle; diff --git a/src/js/SpectraDisplayer.js b/src/js/SpectraDisplayer.js index 24e638ce..d2c783db 100644 --- a/src/js/SpectraDisplayer.js +++ b/src/js/SpectraDisplayer.js @@ -120,6 +120,12 @@ export class SpectraDisplayer extends DOMElement { } }; + updateCanvas() { + this.divNode.querySelectorAll("canvas").forEach(canvas => { + canvas.width = this.view.aladin.aladinDiv.getBoundingClientRect().width + }); + } + constructor(view, options) { super() @@ -142,7 +148,7 @@ export class SpectraDisplayer extends DOMElement { this.scaleX = undefined; this.scaleY = undefined; this.height = options && options.height || 300; - this.width = options && options.width || 600; + this.width = options && options.width || view.aladin.aladinDiv.getBoundingClientRect().width; this.minY = undefined; this.maxY = undefined; this.mouseFreq = undefined; @@ -167,19 +173,22 @@ export class SpectraDisplayer extends DOMElement { name: "unit selector", value: "f", type: 'select', - classList: ['aladin-spectra-unit-selector'], + classList: ['aladin-spectra-unit'], options: [ SpectraDisplayer.UNIT.FREQUENCY.label, SpectraDisplayer.UNIT.WAVELENGTH.label, SpectraDisplayer.UNIT.VELOCITY.label, ], tooltip: { - content: `Unit: ${SpectraDisplayer.UNIT.FREQUENCY.label}, ${SpectraDisplayer.UNIT.WAVELENGTH.label} and ${SpectraDisplayer.UNIT.VELOCITY.label}`, - position: {direction: "right"} + aladin: view.aladin, + global: true, + content: `Unit: Hz, m or km/s`, }, change: (e) => { let label = e.target.value; + let prevUnit = self.unit; + if (label === SpectraDisplayer.UNIT.FREQUENCY.label) { self.unit = SpectraDisplayer.UNIT.FREQUENCY } else if (label === SpectraDisplayer.UNIT.WAVELENGTH.label) { @@ -188,7 +197,15 @@ export class SpectraDisplayer extends DOMElement { self.unit = SpectraDisplayer.UNIT.VELOCITY } - self._redrawLabels(); + if (prevUnit === SpectraDisplayer.UNIT.FREQUENCY) { + self.data.values.reverse(); + } else if (prevUnit === SpectraDisplayer.UNIT.WAVELENGTH && self.unit === SpectraDisplayer.UNIT.FREQUENCY) { + self.data.values.reverse(); + } else if (prevUnit === SpectraDisplayer.UNIT.VELOCITY && self.unit === SpectraDisplayer.UNIT.FREQUENCY) { + self.data.values.reverse(); + } + + self._redraw(self.ctx); }, }) @@ -199,8 +216,9 @@ export class SpectraDisplayer extends DOMElement { url: HomeIconUrl }, tooltip: { - content: "Scale for data", - position: {direction: "right"} + aladin: view.aladin, + global: true, + content: `Auto scale to fit window height`, }, classList: ['aladin-spectra-home'], action(e) { @@ -227,24 +245,23 @@ export class SpectraDisplayer extends DOMElement { this.unit = SpectraDisplayer.UNIT.FREQUENCY; let divNode = document.createElement("div"); - divNode.style.position = "absolute"; - divNode.style.left = "50%"; - divNode.style.transform = "translateX(-50%)"; - divNode.style.bottom = "2px"; - divNode.style.width = this.width + "px"; + //divNode.style.width = this.width + "px"; divNode.style.height = this.height + "px"; - divNode.classList.add("aladin-lite-spectra-displayer") + divNode.classList.add("aladin-spectra-displayer") divNode.appendChild(this.canvas) divNode.appendChild(canvasCursor) divNode.appendChild(canvasLabels) divNode.appendChild(unitSelector.element()) divNode.appendChild(autoCenterBtn.element()) - //divNode.appendChild(extractionBtn.element()) this.divNode = divNode; - this.view.aladin.aladinDiv.appendChild(divNode); + let statusBar = this.view.aladin.statusBar; + this.view.aladin.aladinDiv.insertBefore( + divNode, + statusBar && statusBar.element() + ) this.defineEventListeners() this.hips3DList = new Map(); @@ -381,7 +398,8 @@ export class SpectraDisplayer extends DOMElement { canvas.style.cursor = 'default'; - this.ctxCursor.clearRect(0, 0, this.width, this.height); + let w = this.view.aladin.aladinDiv.getBoundingClientRect().width; + this.ctxCursor.clearRect(0, 0, w, this.height); this.mouseFreq = null; if (my >= v) { @@ -398,12 +416,18 @@ export class SpectraDisplayer extends DOMElement { let curFreq = self.hips.getFrequency(); let curHash = Number(self.view.wasm.freq2hash(self.hips.layer, curFreq)); - let mouseHash = curHash + Math.round((mx - (this.width / 2)) / this.scaleX) + let mouseHash; + if (self.unit === SpectraDisplayer.UNIT.FREQUENCY) { + mouseHash = curHash + Math.round((mx - (w / 2)) / this.scaleX); + } else { + mouseHash = curHash - Math.round((mx - (w / 2)) / this.scaleX); + } this.mouseFreq = self.view.wasm.hash2freq(self.hips.layer, BigInt(mouseHash)); } this._redrawLabels() + if (!this.isDragging) { // Draw the vertical line that can be grabed to move the slice this.ctx.beginPath(); @@ -416,6 +440,12 @@ export class SpectraDisplayer extends DOMElement { this.canvas.style.cursor = 'grab'; } + if (my >= v) { + this.lastMouse = { x: mx, y: my }; + } else { + this.lastMouse = undefined; + } + return; } @@ -429,28 +459,35 @@ export class SpectraDisplayer extends DOMElement { // look where we are in the freq range let j = Utils.binarySearch(self.data.freqs, self.data.freq); - let df, f; + let df, f, f0; if (j > 0 && j < self.data.freqs.length - 1) { df = (self.data.freqs[j + 1] - self.data.freqs[j - 1]) * 0.5; - f = self.data.freq - dx * df; + f0 = self.data.freq; } else if (j == 0) { df = self.data.freqs[1] - self.data.freqs[0] - f = self.data.freqs[0] - dx * df; + f0 = self.data.freq[0]; } else { df = self.data.freqs[self.data.freqs.length - 1] - self.data.freqs[self.data.freqs.length - 2]; - f = self.data.freqs[self.data.freqs.length - 1] - dx * df; + f0 = self.data.freqs[self.data.freqs.length - 1]; + } + + if (this.unit === SpectraDisplayer.UNIT.FREQUENCY) { + f = f0 - dx * df; + } else { + f = f0 + dx * df; } self.hips.setFrequency({ value: f, unit: 'Hz' }) - this.lastMouse = { x: mx, y: my }; } }); Utils.on(canvas, 'mouseup touchend', (e) => { + let w = this.view.aladin.aladinDiv.getBoundingClientRect().width; + if (!this.enabled) { let paramsEvent = { bubbles: e.bubbles, @@ -496,20 +533,27 @@ export class SpectraDisplayer extends DOMElement { if (my >= v) { let dx = (mx - rect.width * 0.5) / this.scaleX; if (dx != 0) { + // Set the frequency // look where we are in the freq range let j = Utils.binarySearch(self.data.freqs, self.data.freq); - let df, f; + let df, f, f0; if (j > 0 && j < self.data.freqs.length - 1) { df = (self.data.freqs[j + 1] - self.data.freqs[j - 1]) * 0.5; - f = self.data.freq + dx * df; + f0 = self.data.freq; } else if (j == 0) { df = self.data.freqs[1] - self.data.freqs[0] - f = self.data.freqs[0] + dx * df; + f0 = self.data.freq[0]; } else { df = self.data.freqs[self.data.freqs.length - 1] - self.data.freqs[self.data.freqs.length - 2]; - f = self.data.freqs[self.data.freqs.length - 1] + dx * df; + f0 = self.data.freqs[self.data.freqs.length - 1]; + } + + if (this.unit === SpectraDisplayer.UNIT.FREQUENCY) { + f = f0 + dx * df; + } else { + f = f0 - dx * df; } self.hips.setFrequency({ @@ -517,11 +561,11 @@ export class SpectraDisplayer extends DOMElement { unit: 'Hz' }) } - this.lastMouse = { x: mx, y: my }; + } - //this.ctxCursor.clearRect(0, 0, this.width, this.height); + //this.ctxCursor.clearRect(0, 0, w, this.height); this.mouseFreq = null; } @@ -542,7 +586,13 @@ export class SpectraDisplayer extends DOMElement { }); Utils.on(canvas, 'wheel', (e) => { - this.ctxCursor.clearRect(0, 0, this.width, this.height); + // stop the propagation to prevent scrolling on the page + e.preventDefault(); + e.stopPropagation(); + + let w = this.view.aladin.aladinDiv.getBoundingClientRect().width; + + this.ctxCursor.clearRect(0, 0, w, this.height); const wheelEvent = new WheelEvent('wheel', { bubbles: true, @@ -594,7 +644,10 @@ export class SpectraDisplayer extends DOMElement { let data = event.detail; if (data.layer === this.hips.layer) { this.data = data; - console.log(data) + if (this.unit !== SpectraDisplayer.UNIT.FREQUENCY) { + this.data.values.reverse(); + } + this._redraw(this.ctx); } }; @@ -623,11 +676,14 @@ export class SpectraDisplayer extends DOMElement { } _redraw() { + const values = this.data.values; let len = values.length; // Clear previous drawing - this.ctx.clearRect(0, 0, this.width, this.height); + let w = this.view.aladin.aladinDiv.getBoundingClientRect().width; + + this.ctx.clearRect(0, 0, w, this.height); // Find min and max for scaling let valuesWithNoNans = values.filter(v=>Number.isFinite(v)); @@ -643,7 +699,7 @@ export class SpectraDisplayer extends DOMElement { this.maxY = Math.max(...valuesWithNoNans) } - this.scaleX = this.width / (len - 1); + this.scaleX = w / (len - 1); this.scaleY = (this.maxY - this.minY === 0) ? 1 : this.height / (this.maxY - this.minY); this._redrawSpectra(values) @@ -656,15 +712,18 @@ export class SpectraDisplayer extends DOMElement { this.ctx.lineWidth = 2; this.ctx.stroke(); - this.ctxCursor.clearRect(0, 0, this.width, this.height); - this.ctxCursor.beginPath(); - this.ctxCursor.moveTo(this.lastMouse.x, this.height); - let v = this.data.values[Math.round(this.lastMouse.x / this.scaleX)] - v = this.height - (v - this.minY) * this.scaleY - this.ctxCursor.lineTo(this.lastMouse.x, v); - this.ctxCursor.strokeStyle = "yellow"; - this.ctxCursor.lineWidth = 2; - this.ctxCursor.stroke() + this.ctxCursor.clearRect(0, 0, w, this.height); + + if (this.lastMouse) { + this.ctxCursor.beginPath(); + this.ctxCursor.moveTo(this.lastMouse.x, this.height); + let v = this.data.values[Math.round(this.lastMouse.x / this.scaleX)] + v = this.height - (v - this.minY) * this.scaleY; + this.ctxCursor.lineTo(this.lastMouse.x, v); + this.ctxCursor.strokeStyle = "yellow"; + this.ctxCursor.lineWidth = 2; + this.ctxCursor.stroke() + } this._redrawLabels() } @@ -697,17 +756,20 @@ export class SpectraDisplayer extends DOMElement { if (Math.abs(value) >= 1 || unit === units[units.length - 1].unit) { // Calculate number of decimal places needed to show the given precision - const decimals = Math.max(0, Math.ceil(-Math.log10(precisionInUnit))); + const decimals = Math.min(8, Math.max(0, Math.ceil(-Math.log10(precisionInUnit)))); return value.toFixed(decimals) + " " + unit; } } } + let w = this.view.aladin.aladinDiv.getBoundingClientRect().width; + // Clear previous drawing - this.ctxLabels.clearRect(0, 0, this.width, this.height); + this.ctxLabels.clearRect(0, 0, w, this.height); let drawLabel = (ctx, str, x, y, strokeStyle, font, fillStyle) => { ctx.strokeStyle = strokeStyle; // contour color + ctx.lineWidth = 3; ctx.strokeText(str, x, y); ctx.fillStyle = fillStyle; @@ -722,11 +784,14 @@ export class SpectraDisplayer extends DOMElement { // min window freq this.ctxLabels.textAlign = "left"; // Horizontally centered + let fmin = spectraValue2String(this.data.freqMin, this.data.freqs[1] - this.data.freqs[0]); + let fmax = spectraValue2String(this.data.freqMax, this.data.freqs[this.data.freqs.length - 1] - this.data.freqs[this.data.freqs.length - 2]); + drawLabel( this.ctxLabels, - spectraValue2String(this.data.freqMin, this.data.freqs[1] - this.data.freqs[0]), + this.unit === SpectraDisplayer.UNIT.FREQUENCY ? fmin : fmax, 0, - this.height - 20, + this.height - 60, 'black', '20px monospace', 'lightgreen' @@ -736,9 +801,9 @@ export class SpectraDisplayer extends DOMElement { this.ctxLabels.textAlign = "right"; // Horizontally centered drawLabel( this.ctxLabels, - spectraValue2String(this.data.freqMax, this.data.freqs[this.data.freqs.length - 1] - this.data.freqs[this.data.freqs.length - 2]), - this.width, - this.height - 20, + this.unit === SpectraDisplayer.UNIT.FREQUENCY ? fmax : fmin, + w, + this.height - 60, 'black', '20px monospace', 'lightgreen' @@ -757,8 +822,8 @@ export class SpectraDisplayer extends DOMElement { drawLabel( this.ctxLabels, str, - this.width / 2, - this.height - 20, + w / 2, + this.height - 10, 'black', '20px monospace', fillStyle diff --git a/src/js/Utils.ts b/src/js/Utils.ts index c524c82a..71669471 100644 --- a/src/js/Utils.ts +++ b/src/js/Utils.ts @@ -431,6 +431,23 @@ Utils.fetch = function(params) { }) } +Utils.fetchWithProxy = function(params) { + return Utils.fetch({ + ...params, + error: (e) => { + console.error(e) + console.info("Trying querying the proxy:" + JSONP_PROXY) + // Try as CORS + const url = JSONP_PROXY + '?url=' + params.url; + console.log(url) + return Utils.fetch({ + ...params, + url, + }); + } + }) +} + Utils.on = function(element, events, callback) { events.split(' ') .forEach(e => { diff --git a/src/js/View.js b/src/js/View.js index 5f9ea7b5..1b404872 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -105,6 +105,11 @@ export let View = (function () { this.debounceResize = Utils.debounce(() => { self.wasm.resize(self.width, self.height); self.updateZoomState() + + if (self.spectraDisplayer) { + self.spectraDisplayer.updateCanvas() + self.spectraDisplayer._redraw() + } }, 2); // Attach the drag and drop events to the view @@ -318,7 +323,7 @@ export let View = (function () { Object.defineProperties(this, { fov: { get() { - return this.wasm.getFieldOfView(); + return this.wasm.getFieldOfView()[0]; }, set(newFov) { this.setFoV(newFov); @@ -601,7 +606,7 @@ export let View = (function () { if (imageLayer.dataproductType === "spectral-cube") { if (!this.spectraDisplayer) { - this.spectraDisplayer = new SpectraDisplayer(this, {width: 800, height: 300}); + this.spectraDisplayer = new SpectraDisplayer(this, {height: 250}); } this.spectraDisplayer.attachHiPS3D(imageLayer) @@ -641,16 +646,19 @@ export let View = (function () { // prevent default context menu from appearing (potential clash with right-click cuts control) Utils.on(view.catalogCanvas, "contextmenu", function (e) { e.preventDefault(); - let ctxMenu = view.aladin.contextMenu; - if(ctxMenu) { - e.stopPropagation(); - if (!view.rightClick && showContextMenu) { - ctxMenu.attach( - DefaultActionsForContextMenu.getDefaultActions(view.aladin), - null - ); - ctxMenu._show({e}); - } + + if (view.aladin.options.showContextMenu) { + let ctxMenu = view.aladin.contextMenu; + if(ctxMenu) { + e.stopPropagation(); + if (!view.rightClick && showContextMenu) { + ctxMenu.attach( + DefaultActionsForContextMenu.getDefaultActions(view.aladin), + null + ); + ctxMenu._show({e}); + } + } } }); @@ -941,7 +949,7 @@ export let View = (function () { if (view.rightClick) { let ctxMenu = view.aladin.contextMenu; - if (showContextMenu && ctxMenu) { + if (showContextMenu && ctxMenu && view.aladin.options.showContextMenu) { ctxMenu.attach( DefaultActionsForContextMenu.getDefaultActions(view.aladin), null @@ -1760,10 +1768,12 @@ export let View = (function () { this.computeNorder(); let fovX = this.fov; - let fovY = this.height / this.width * fovX; + let fovY = this.wasm.getFieldOfView()[1]; fovX = Math.min(fovX, 360); fovY = Math.min(fovY, 180); + this.fovY = fovY; + this.debounceProgCatOnZoom(); ALEvent.ZOOM_CHANGED.dispatchedTo(this.aladinDiv, { fovX, fovY }); @@ -1801,27 +1811,6 @@ export let View = (function () { // register its promise this.imageLayersBeingQueried.set(layer, imageLayer); - // Check whether this layer already exist - const idxOverlayLayer = this.overlayLayers.findIndex(overlayLayer => overlayLayer == layer); - let alreadyPresentImageLayer; - if (idxOverlayLayer == -1) { - // it does not exist so we add it to the stack - this.overlayLayers.push(layer); - } else { - // it exists - alreadyPresentImageLayer = this.imageLayers.get(layer); - - if (alreadyPresentImageLayer) { - if (alreadyPresentImageLayer.added === true) { - ALEvent.LAYER_REMOVED.dispatchedTo(this.aladinDiv, { layer: alreadyPresentImageLayer }); - } - - alreadyPresentImageLayer.added = false; - } - // Notify that this image layer has been replaced by the wasm part - this.imageLayers.delete(layer); - } - this.addImageLayer(imageLayer, layer); return imageLayer; @@ -1833,6 +1822,25 @@ export let View = (function () { const layer = imageLayer.layer; imageLayer.added = true; + const idxOverlayLayer = this.overlayLayers.findIndex(overlayLayer => overlayLayer == layer); + if (idxOverlayLayer === -1) { + this.overlayLayers.push(layer); + } else { + // layer already present + // it exists + var alreadyPresentImageLayer = this.imageLayers.get(layer); + + if (alreadyPresentImageLayer) { + //let idx = this.removeImageLayer(layer) + //if (idx >= 0) { + // this.overlayLayers.splice(idx, 0, layer); + //} + } + + // Notify that this image layer has been replaced by the wasm part + //this.imageLayers.delete(layer); + } + this.imageLayers.set(layer, imageLayer); // select the layer if he is on top @@ -1841,6 +1849,21 @@ export let View = (function () { ALEvent.LAYER_ADDED.dispatchedTo(this.aladinDiv, { layer: imageLayer }); } + View.prototype._waitsForLayer = function() { + return this.promises.length !== 0; + } + + View.prototype.getFirstLayer = function() { + let layer = this.overlayLayers && this.overlayLayers[0]; + + if (!layer) { + // the overlay has not yet been added and is currently queried. + layer = this.imageLayersBeingQueried.keys().next().value; + } + + return layer; + } + View.prototype.addImageLayer = function (imageLayer, layer) { let self = this; // start the query @@ -1883,6 +1906,8 @@ export let View = (function () { imageLayer.errorCallback(e); } + this.removeImageLayer(imageLayer); + throw e; }) .finally(() => { @@ -1894,8 +1919,15 @@ export let View = (function () { // Remove the settled promise this.promises.splice(idx, 1); - const noMoreLayersToWaitFor = this.promises.length === 0; - if (noMoreLayersToWaitFor && self.empty) { + const waitsForLayer = this._waitsForLayer(); + if (!waitsForLayer && this.delayedBaseLayerCalledParams && this.delayedBaseLayerCalledParams !== layer) { + this.aladin.setBaseImageLayer(this.delayedBaseLayerCalledParams) + this.delayedBaseLayerCalledParams = null; + + return; + } + + if (!waitsForLayer && self.empty) { // no promises to launch and the view has no HiPS. // This situation can occurs if the MOCServer is out // If so we can directly put the url of the DSS hosted in alasky, @@ -1933,7 +1965,7 @@ export let View = (function () { if (imageLayer === undefined) { // there is nothing to remove - return; + return -1; } // Update the backend @@ -1941,26 +1973,22 @@ export let View = (function () { // Get the survey to remove to dissociate it from the view imageLayer.added = false; - // Delete it - this.imageLayers.delete(layer); const idxOverlaidLayer = this.overlayLayers.findIndex(overlaidLayer => overlaidLayer == layer); - if (idxOverlaidLayer == -1) { - // layer not found - return; - } + // Delete it + this.imageLayers.delete(layer); // Remove it from the layer stack this.overlayLayers.splice(idxOverlaidLayer, 1); - if (this.overlayLayers.length === 0) { - //this.empty = true; - } else if (this.selectedLayer === layer) { + if (this.overlayLayers.length > 0 && this.selectedLayer === layer) { // If the layer removed was selected then we select the last layer this.selectLayer(this.overlayLayers[this.overlayLayers.length - 1]); } ALEvent.LAYER_REMOVED.dispatchedTo(this.aladinDiv, { layer: imageLayer }); + + return idxOverlaidLayer; }; View.prototype.contains = function(survey) { @@ -2014,8 +2042,7 @@ export let View = (function () { let obj = imageLayer || imageLayerQueried; - if (obj && obj.added) - return obj; + return obj; }; View.prototype.requestRedraw = function () { diff --git a/src/js/events/ALEvent.js b/src/js/events/ALEvent.js index 8241542a..fe02bcec 100644 --- a/src/js/events/ALEvent.js +++ b/src/js/events/ALEvent.js @@ -58,7 +58,7 @@ export class ALEvent { static LAYER_CHANGED = new ALEvent("AL:Layer.changed"); static HIPS_CACHE_UPDATED = new ALEvent("AL:HiPSCache.updated"); - static FAVORITE_HIPS_LIST_UPDATED = new ALEvent("AL:HiPSFavorites.updated"); + static FAVORITE_LAYERS_LIST_UPDATED = new ALEvent("AL:HiPSFavorites.updated"); static GRAPHIC_OVERLAY_LAYER_ADDED = new ALEvent("AL:GraphicOverlayLayer.added"); static GRAPHIC_OVERLAY_LAYER_REMOVED = new ALEvent("AL:GraphicOverlayLayer.removed"); diff --git a/src/js/gui/Box/CatalogQueryBox.js b/src/js/gui/Box/CatalogQueryBox.js index 27cda75c..afa6e0c8 100644 --- a/src/js/gui/Box/CatalogQueryBox.js +++ b/src/js/gui/Box/CatalogQueryBox.js @@ -43,15 +43,19 @@ import { ActionButton } from "../Widgets/ActionButton.js"; export class CatalogQueryBox extends Box { static catalogs = {}; + static catTitle2Id = {}; constructor(aladin, options) { // Query the mocserver MocServer.getAllCatalogHiPSes() .then((catalogs) => { catalogs.forEach((cat) => { - CatalogQueryBox.catalogs[cat.obs_title] = cat; + //console.log(cat) + CatalogQueryBox.catalogs[cat.ID] = cat; + CatalogQueryBox.catTitle2Id[cat.obs_title] = cat.ID; }); - searchDropdown.update({options: Object.keys(CatalogQueryBox.catalogs)}) + //console.log(CatalogQueryBox.catalogs) + searchDropdown.update({options: Object.keys(CatalogQueryBox.catTitle2Id)}) }) const fnIdSelected = function(type, params) { @@ -130,7 +134,8 @@ import { ActionButton } from "../Widgets/ActionButton.js"; }) } catch (e) { // Or he can select a HiPS from the list given - const catalog = CatalogQueryBox.catalogs[value]; + const catID = CatalogQueryBox.catTitle2Id[value]; + const catalog = CatalogQueryBox.catalogs[catID]; if (catalog) { self._selectItem(catalog, aladin); diff --git a/src/js/gui/Box/HiPSCompositeBox.js b/src/js/gui/Box/HiPSCompositeBox.js index 6ca40f12..39ddd51f 100644 --- a/src/js/gui/Box/HiPSCompositeBox.js +++ b/src/js/gui/Box/HiPSCompositeBox.js @@ -22,7 +22,7 @@ import hipsIconUrl from "../../../../assets/icons/hips.svg"; import addIconUrl from "../../../../assets/icons/plus.svg"; import settingsIconUrl from "../../../../assets/icons/settings.svg"; import { Icon } from "../Widgets/Icon.js"; -import { HiPSSelector } from "../Input/HiPSSelector.js"; +import { LayerSelector } from "../Input/LayerSelector.js"; import { HiPSBrowserBox } from "./HiPSBrowserBox.js"; import { Input } from "../Widgets/Input.js"; import { ActionButton } from "../Widgets/ActionButton.js"; @@ -131,7 +131,7 @@ export class HiPSCompositeBox extends Box { this.hipsOptions.push({}); let self = this; let newLayerLayout = [ - new HiPSSelector({ + new LayerSelector({ change(e) { let name = e.target.value; let idLayer = getIdHiPS(e.target); @@ -149,7 +149,7 @@ export class HiPSCompositeBox extends Box { }); } else { // it is an hips - let HiPSOptions = HiPSSelector.cachedHiPS[name]; + let HiPSOptions = LayerSelector.cachedLayers[name]; self.hipsOptions[idLayer].id = HiPSOptions.id || HiPSOptions.url } diff --git a/src/js/gui/Box/HiPSSettingsBox.js b/src/js/gui/Box/HiPSSettingsBox.js index 5f6152a6..31d0c045 100644 --- a/src/js/gui/Box/HiPSSettingsBox.js +++ b/src/js/gui/Box/HiPSSettingsBox.js @@ -40,6 +40,7 @@ import { Form } from "../Widgets/Form.js"; import waveOnIconUrl from '../../../../assets/icons/wave-on.svg'; import { WidgetTogglerButton } from "../Button/Toggler.js"; import { Layout } from "../Layout.js"; + import { Utils } from "../../Utils"; export class HiPSSettingsBox extends Box { // Constructor @@ -204,7 +205,7 @@ import { WidgetTogglerButton } from "../Button/Toggler.js"; value: 'linear', options: ['sqrt', 'linear', 'asinh', 'pow2', 'log'], change(e) { - self.options.layer.setColormap(self.options.layer.getColorCfg().getColormap(), {stretch: e.target.value}); + self.options.layer.setColormap(self.options.layer.getColormap(), {stretch: e.target.value}); }, tooltip: {content: 'stretch function', position: {direction: 'bottom'}} }, @@ -216,7 +217,7 @@ import { WidgetTogglerButton } from "../Button/Toggler.js"; value: 0.0, change: (e) => { let minCut = +e.target.value - self.options.layer.setCuts(minCut, self.options.layer.getColorCfg().getCuts()[1]) + self.options.layer.setCuts(minCut, self.options.layer.getCuts()[1]) }, cssStyle: { width: '7rem' } }, @@ -228,7 +229,7 @@ import { WidgetTogglerButton } from "../Button/Toggler.js"; value: 1.0, change: (e) => { let maxCut = +e.target.value - self.options.layer.setCuts(self.options.layer.getColorCfg().getCuts()[0], maxCut) + self.options.layer.setCuts(self.options.layer.getCuts()[0], maxCut) }, cssStyle: { width: '7rem' } }] @@ -281,17 +282,19 @@ import { WidgetTogglerButton } from "../Button/Toggler.js"; } _update(layer) { - let colorCfg = layer.getColorCfg(); - let stretch = colorCfg.stretch; - let colormap = colorCfg.getColormap(); - let reversed = colorCfg.getReversed(); + let stretch = layer.stretch; + let colormap = layer.getColormap(); + let reversed = layer.getReversed(); - let [minCut, maxCut] = colorCfg.getCuts(); - if (minCut) + let [minCut, maxCut] = layer.getCuts(); + if (Utils.isNumber(minCut)) { this.pixelSettingsContent.set('mincut', +minCut.toFixed(4)) - if (maxCut) + } + + if (Utils.isNumber(maxCut)) { this.pixelSettingsContent.set('maxcut', +maxCut.toFixed(4)) - + } + this.pixelSettingsContent.set('stretch', stretch) let fmtInput = this.pixelSettingsContent.getInput('fmt') fmtInput.innerHTML = ''; @@ -309,10 +312,10 @@ import { WidgetTogglerButton } from "../Button/Toggler.js"; this.colorSettingsContent.set('reverse', reversed); this.opacitySettingsContent.set('opacity', layer.getOpacity()); - this.luminositySettingsContent.set('brightness', colorCfg.getBrightness()); - this.luminositySettingsContent.set('contrast', colorCfg.getContrast()); - this.luminositySettingsContent.set('gamma', colorCfg.getGamma()); - this.luminositySettingsContent.set('saturation', colorCfg.getSaturation()); + this.luminositySettingsContent.set('brightness', layer.getBrightness()); + this.luminositySettingsContent.set('contrast', layer.getContrast()); + this.luminositySettingsContent.set('gamma', layer.getGamma()); + this.luminositySettingsContent.set('saturation', layer.getSaturation()); } update(options) { diff --git a/src/js/gui/Box/StackBox.js b/src/js/gui/Box/StackBox.js index f6ced4cf..5eab54a8 100644 --- a/src/js/gui/Box/StackBox.js +++ b/src/js/gui/Box/StackBox.js @@ -58,7 +58,7 @@ import { HiPSCompositeBox } from "./HiPSCompositeBox.js" import { Catalog } from "../../Catalog.js"; import { ProgressiveCat } from "../../ProgressiveCat.js"; import { Form } from "../Widgets/Form.js"; -import { HiPSSelector } from "./../Input/HiPSSelector.js"; +import { LayerSelector } from "../Input/LayerSelector.js"; import { HiPS } from "../../HiPS.js"; export class OverlayStackBox extends Box { @@ -541,11 +541,6 @@ export class OverlayStackBox extends Box { aladin.hipsBrowser._show({ selected: (hips) => { - let oldHiPS = aladin.getOverlayImageLayer(newLayer); - if (oldHiPS && hips.id === oldHiPS.id) { - return; - } - aladin.setOverlayImageLayer(hips, newLayer); }, position: { @@ -912,9 +907,10 @@ export class OverlayStackBox extends Box { } } - let spectraDisplayer = aladin.view.spectraDisplayer; - if (spectraDisplayer) + /*let spectraDisplayer = aladin.view.spectraDisplayer; + if (spectraDisplayer) { spectraDisplayer.attachHiPS3D(options.layer) + } */ }, widget: catSettingsBox, openDirection: "right" @@ -967,7 +963,7 @@ export class OverlayStackBox extends Box { continue; } - let HiPSselect = new HiPSSelector({ + let layerSelect = new LayerSelector({ layer: hips, change(e) { let name = e.target.value; @@ -976,10 +972,12 @@ export class OverlayStackBox extends Box { if (!aladin.hipsBrowser) { aladin.hipsBrowser = new HiPSBrowserBox(aladin); } - + + let newLayer = Utils.uuidv4(); + aladin.hipsBrowser._show({ selected: (hips) => { - self.aladin.setOverlayImageLayer(hips, hips.layer); + self.aladin.setOverlayImageLayer(hips, newLayer); }, position: { anchor: "center center" } }); @@ -987,10 +985,15 @@ export class OverlayStackBox extends Box { } let overlayLayer; - if (name in HiPSSelector.cachedHiPS) { + if (name in LayerSelector.cachedLayers) { // it is an hips - let HiPSOptions = HiPSSelector.cachedHiPS[name]; - overlayLayer = A.HiPS(HiPSOptions.id || HiPSOptions.url, HiPSOptions); + let layerOptions = LayerSelector.cachedLayers[name]; + //if (layerOptions.type === "hips") { + // overlayLayer = A.HiPS(layerOptions.id || layerOptions.url, layerOptions); + //} else if (layerOptions.type === "image") { + // overlayLayer = A.image(layerOptions.url, layerOptions); + //} + overlayLayer = layerOptions.id; } else { overlayLayer = hips } @@ -1121,12 +1124,12 @@ export class OverlayStackBox extends Box { } btns = btns.concat([swapBtn, deleteBtn]); - let item = Layout.horizontal([HiPSselect, Layout.horizontal(btns)]); + let item = Layout.horizontal([layerSelect, Layout.horizontal(btns)]); layout.push(item); if (!(hips.layer in self.ui)) { self.ui[hips.layer] = { - HiPSSelector: HiPSselect, + //layerSelector: layerSelect, settingsBox, settingsBtn, showBtn, diff --git a/src/js/gui/Button/CtxMenuOpener.js b/src/js/gui/Button/CtxMenuOpener.js index 7e479086..87a6179b 100644 --- a/src/js/gui/Button/CtxMenuOpener.js +++ b/src/js/gui/Button/CtxMenuOpener.js @@ -51,7 +51,7 @@ export class CtxMenuActionButtonOpener extends WidgetTogglerButton { widget: aladin.contextMenu, enable(e) { // If it was hidden then reopen it - if (self.layout) { + if (self.layout && self.ctxMenu) { self.ctxMenu.attach(self.layout, self) } }, diff --git a/src/js/gui/Button/Toggler.js b/src/js/gui/Button/Toggler.js index c322560f..b23f659f 100644 --- a/src/js/gui/Button/Toggler.js +++ b/src/js/gui/Button/Toggler.js @@ -113,9 +113,11 @@ export class TogglerActionButton extends ActionButton { if (enable) enable(o) - widget._show({ - position: self.position - }) + if (widget) { + widget._show({ + position: self.position + }) + } }, off: (_) => { self.close(); @@ -126,12 +128,15 @@ export class TogglerActionButton extends ActionButton { this.update(options) - widget.setToggler(this); + if (widget) + widget.setToggler(this); + this.widget = widget; } close() { - this.widget._hide(); + if (this.widget) + this.widget._hide(); super.close() } diff --git a/src/js/gui/FoV.js b/src/js/gui/FoV.js index b3216378..778275dd 100644 --- a/src/js/gui/FoV.js +++ b/src/js/gui/FoV.js @@ -23,9 +23,9 @@ /****************************************************************************** * Aladin Lite project * - * File Location.js + * File FoV.js * - * Author: Thomas Boch[CDS] + * Author: Matthieu Baumann[CDS] * *****************************************************************************/ import { Numbers } from "../libs/astro/coo.js"; @@ -48,7 +48,7 @@ export class FoV extends DOMElement { let zoomIn = new ActionButton({ classList: 'aladin-zoom-in', size: 'small', - tooltip: {content: 'zoom in', position: {direction: 'top'}}, + tooltip: {content: 'zoom in', position: {direction: 'left'}}, icon: { monochrome: true, size: 'small', @@ -61,7 +61,7 @@ export class FoV extends DOMElement { let zoomOut = new ActionButton({ size: 'small', classList: 'aladin-zoom-out', - tooltip: {content: 'zoom out', position: {direction: 'top'}}, + tooltip: {content: 'zoom out', position: {direction: 'left'}}, icon: { monochrome: true, size: 'small', @@ -74,14 +74,23 @@ export class FoV extends DOMElement { zoomIn.el.classList.add('aladin-zoom-in'); zoomOut.el.classList.add('aladin-zoom-out'); - layout.push(zoomOut) - layout.push(zoomIn) + let aladinZoomDiv = document.createElement("div") + aladinZoomDiv.classList.add('aladin-zoom') + aladinZoomDiv.appendChild(zoomIn.element()); + aladinZoomDiv.appendChild(zoomOut.element()); + + aladin.aladinDiv.appendChild(aladinZoomDiv); + + //layout.push(zoomOut) + //layout.push(zoomIn) } if (options.showFov) { - layout.push(...['
    ', - '
    ×
    ', - '
    ']) + layout.push( + '
    ' + + '
    ×
    ' + + '
    ' + ) } let el = Layout.horizontal(layout); diff --git a/src/js/gui/Input/HiPSSelector.js b/src/js/gui/Input/LayerSelector.js similarity index 70% rename from src/js/gui/Input/HiPSSelector.js rename to src/js/gui/Input/LayerSelector.js index e53367b0..d983feac 100644 --- a/src/js/gui/Input/HiPSSelector.js +++ b/src/js/gui/Input/LayerSelector.js @@ -20,7 +20,7 @@ /****************************************************************************** * Aladin Lite project * - * File gui/Input/HiPSSelector.js + * File gui/Input/LayerSelector.js * * * Author: Matthieu Baumann[CDS] @@ -30,14 +30,14 @@ import { ALEvent } from "../../events/ALEvent.js"; import { Input } from "../Widgets/Input.js"; -export class HiPSSelector extends Input { - static cachedHiPS = {} +export class LayerSelector extends Input { + static cachedLayers = {} static objects = []; // constructor constructor(options) { let surveys = []; - for (var survey of Object.values(HiPSSelector.cachedHiPS)) { + for (var survey of Object.values(LayerSelector.cachedLayers)) { surveys.push({value: survey.id, label: survey.name}); } surveys.sort((s1, s2) => { @@ -82,31 +82,31 @@ export class HiPSSelector extends Input { self = this; - HiPSSelector.objects.push(self); + LayerSelector.objects.push(self); } }; (function () { - ALEvent.FAVORITE_HIPS_LIST_UPDATED.listenedBy(document, (event) => { - let favoritesHips = event.detail; + ALEvent.FAVORITE_LAYERS_LIST_UPDATED.listenedBy(document, (event) => { + let favoritesLayer = event.detail; - HiPSSelector.cachedHiPS = {}; + LayerSelector.cachedLayers = {}; - for (var hips of favoritesHips) { - let key = hips.id || hips.url || hips.name; - HiPSSelector.cachedHiPS[key] = hips; + for (var layer of favoritesLayer) { + let key = layer.id || layer.url || layer.name; + LayerSelector.cachedLayers[key] = layer; } // Update the options of the selector - let favoritesHiPS = [] - for(var hips of Object.values(HiPSSelector.cachedHiPS)) { - favoritesHiPS.push({ - value: hips.id, - label: hips.name + let favorites = [] + for(var layer of Object.values(LayerSelector.cachedLayers)) { + favorites.push({ + value: layer.id, + label: layer.name }) } - favoritesHiPS.sort((s1, s2) => { + favorites.sort((s1, s2) => { const s1l = s1.label.toLowerCase() const s2l = s2.label.toLowerCase() @@ -120,28 +120,28 @@ export class HiPSSelector extends Input { return 0; }); - for (var selector of HiPSSelector.objects) { + for (var selector of LayerSelector.objects) { // refers to an HiPS image survey - let currentFavoriteHiPS = { + let currentFavorite = { value: selector.options.value, label: selector.options.label, }; - let favoritesHiPSCopy = [...favoritesHiPS]; + let favoritesCopy = [...favorites]; // Add the current hips to the selector as well, even if it has been manually // removed from the HiPSList - if (!favoritesHiPSCopy.some(item => item.value === currentFavoriteHiPS.value)) { - favoritesHiPSCopy.push(currentFavoriteHiPS) + if (!favoritesCopy.some(item => item.value === currentFavorite.value)) { + favoritesCopy.push(currentFavorite) } - favoritesHiPSCopy.push("More...") + favoritesCopy.push("More...") - currentFavoriteHiPS["title"] = currentFavoriteHiPS["label"]; + currentFavorite["title"] = currentFavorite["label"]; selector.update({ - ...currentFavoriteHiPS, - options: favoritesHiPSCopy + ...currentFavorite, + options: favoritesCopy }); } }); diff --git a/src/js/libs/avm.js b/src/js/libs/avm.js index 8ef5695d..a669a47c 100644 --- a/src/js/libs/avm.js +++ b/src/js/libs/avm.js @@ -6,7 +6,9 @@ * * Licensed under the MPL http://www.mozilla.org/MPL/MPL-1.1.txt * - * S + * This code has been adapted to the use of Aladin Lite. It features: + * - fixes that has been discovered with the testing of multiple images found. + * - The support of PNGs */ export let AVM = (function() { @@ -117,7 +119,13 @@ export let AVM = (function() { const findAVM = (arrayBuffer) => { const view = new DataView(arrayBuffer); - var oAVM = _obj.findAVMinJPEG(view); + // See the magic bytes for JPEG or PNG files + var oAVM; + if (view.getUint8(0) === 0xFF && view.getUint8(1) === 0xD8) + oAVM = _obj.findAVMinJPEG(view); + if (view.getUint8(0) === 0x89 && view.getUint8(1) === 0x50) + oAVM = _obj.findAVMinPNG(view); + _obj.wcs = oAVM || {}; _obj.wcsdata = _obj.wcs !== undefined && Object.keys(_obj.wcs).length > 0; @@ -136,7 +144,6 @@ export let AVM = (function() { findAVM(arrayBuffer) }) } - } function addEvent(oElement, strEvent, fncHandler){ @@ -172,6 +179,77 @@ export let AVM = (function() { } } + AVM.prototype.findAVMinPNG = function(oFile) { + // PNG signature: 8 bytes - 0x89 PNG \r \n 0x1a \n + if ( + oFile.getUint8(0) !== 0x89 || + oFile.getUint8(1) !== 0x50 || // 'P' + oFile.getUint8(2) !== 0x4E || // 'N' + oFile.getUint8(3) !== 0x47 || // 'G' + oFile.getUint8(4) !== 0x0D || + oFile.getUint8(5) !== 0x0A || + oFile.getUint8(6) !== 0x1A || + oFile.getUint8(7) !== 0x0A + ) return false; // not a valid PNG + + var iOffset = 8; // skip the 8-byte PNG signature + var iLength = oFile.byteLength; + + while (iOffset < iLength) { + // Each PNG chunk: [4 bytes length][4 bytes type][data][4 bytes CRC] + var iChunkLength = oFile.getUint32(iOffset, false); // big-endian + + // Read the 4-byte chunk type as a string + var sChunkType = + String.fromCharCode(oFile.getUint8(iOffset + 4)) + + String.fromCharCode(oFile.getUint8(iOffset + 5)) + + String.fromCharCode(oFile.getUint8(iOffset + 6)) + + String.fromCharCode(oFile.getUint8(iOffset + 7)); + + if (sChunkType === 'iTXt' || sChunkType === 'tEXt' || sChunkType === 'zTXt') { + // AVM data is typically stored in an iTXt chunk with keyword "AVM" + // iTXt layout: [keyword]\0[compression flag][compression method][language]\0[translated keyword]\0[text] + // tEXt layout: [keyword]\0[text] + var iDataOffset = iOffset + 8; // skip length (4) + type (4) + var iDataEnd = iDataOffset + iChunkLength; + + // Read the keyword (null-terminated) + var sKeyword = ''; + var i = iDataOffset; + while (i < iDataEnd && oFile.getUint8(i) !== 0x00) { + sKeyword += String.fromCharCode(oFile.getUint8(i)); + i++; + } + + //if (sKeyword === 'AVM') { + // Skip past the null terminator + i++; // now at compression flag (iTXt) or text start (tEXt) + + if (sChunkType === 'iTXt') { + // Skip: compression flag (1) + compression method (1) + i += 2; + // Skip language tag (null-terminated) + while (i < iDataEnd && oFile.getUint8(i) !== 0x00) i++; + i++; // skip null + // Skip translated keyword (null-terminated) + while (i < iDataEnd && oFile.getUint8(i) !== 0x00) i++; + i++; // skip null + } + + // i now points to the start of the actual AVM text data + return this.readAVMDataAsWCS(oFile, i, iDataEnd - i); + //} + } + + if (sChunkType === 'IEND') break; // end of PNG, stop searching + + // Move to the next chunk: length (4) + type (4) + data + CRC (4) + iOffset += 4 + 4 + iChunkLength + 4; + } + + return false; + } + AVM.prototype.readAVMDataAsWCS = function(oFile) { var tags = undefined; @@ -200,7 +278,7 @@ export let AVM = (function() { if (unwindTag(tags['Spatial.Equinox'])) wcs.EQUINOX = +unwindTag(tags['Spatial.Equinox']); - wcs.NAXIS = tags['Spatial.ReferenceDimension'] && +tags['Spatial.ReferenceDimension'].length; + wcs.NAXIS = 2; wcs.NAXIS1 = tags['Spatial.ReferenceDimension'] && +tags['Spatial.ReferenceDimension'][0]; wcs.NAXIS2 = tags['Spatial.ReferenceDimension'] && +tags['Spatial.ReferenceDimension'][1]; @@ -219,9 +297,9 @@ export let AVM = (function() { wcs.CROTA2 = +unwindTag(tags['Spatial.Rotation']); } } - - wcs.CRPIX1 = tags['Spatial.ReferencePixel'] && +tags['Spatial.ReferencePixel'][0]; - wcs.CRPIX2 = tags['Spatial.ReferencePixel'] && +tags['Spatial.ReferencePixel'][1]; + const spatialRef = tags['Spatial.ReferencePixel'] + wcs.CRPIX1 = spatialRef && +spatialRef[0] + wcs.CRPIX2 = spatialRef && +spatialRef[1] wcs.CRVAL1 = tags['Spatial.ReferenceValue'] && +tags['Spatial.ReferenceValue'][0]; wcs.CRVAL2 = tags['Spatial.ReferenceValue'] && +tags['Spatial.ReferenceValue'][1]; diff --git a/src/js/vo/Datalink.js b/src/js/vo/Datalink.js index 160f1ec7..f222b046 100644 --- a/src/js/vo/Datalink.js +++ b/src/js/vo/Datalink.js @@ -163,15 +163,15 @@ export let Datalink = (function() { let idxSlice = +properties.hips_cube_firstframe; let updateSlice = () => { - let colorCfg = aladinInstance.getOverlayImageLayer(layer).getColorCfg(); + let pHiPS = aladinInstance.getOverlayImageLayer(layer); let hips = aladinInstance.setOverlayImageLayer(cubeOnTheFlyUrl + idxSlice, layer) hips.setOptions({ - opacity: colorCfg.opacity, - minCut: colorCfg.minCut, - maxCut: colorCfg.maxCut, - colormap: colorCfg.colormap, - stretch: colorCfg.stretch, - reversed: colorCfg.reversed + opacity: pHiPS.opacity, + minCut: pHiPS.minCut, + maxCut: pHiPS.maxCut, + colormap: pHiPS.colormap, + stretch: pHiPS.stretch, + reversed: pHiPS.reversed }) slicer.update({ From 64042cea5febebc88a5698ddddad025cfd8f69ed Mon Sep 17 00:00:00 2001 From: Thomas Boch Date: Mon, 27 Apr 2026 15:42:10 +0200 Subject: [PATCH 06/17] feat: add AVM tags for PNG write --- src/js/AvmUtils.js | 313 +++++++++++++++++++++++++++++++++++++++++++++ src/js/View.js | 69 +++++++--- 2 files changed, 365 insertions(+), 17 deletions(-) create mode 100644 src/js/AvmUtils.js diff --git a/src/js/AvmUtils.js b/src/js/AvmUtils.js new file mode 100644 index 00000000..c4c29832 --- /dev/null +++ b/src/js/AvmUtils.js @@ -0,0 +1,313 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2013 - UDS/CNRS + +function crc32(bytes) { + let crc = 0xffffffff; + + for (let i = 0; i < bytes.length; i++) { + crc ^= bytes[i]; + + for (let j = 0; j < 8; j++) { + const mask = -(crc & 1); + crc = (crc >>> 1) ^ (0xedb88320 & mask); + } + } + + return (crc ^ 0xffffffff) >>> 0; +} + +function u32be(n) { + return new Uint8Array([ + (n >>> 24) & 0xff, + (n >>> 16) & 0xff, + (n >>> 8) & 0xff, + n & 0xff, + ]); +} + +function concatUint8Arrays(arrays) { + const total = arrays.reduce((sum, array) => sum + array.length, 0); + const out = new Uint8Array(total); + let offset = 0; + + for (const array of arrays) { + out.set(array, offset); + offset += array.length; + } + + return out; +} + +function escapeXml(value) { + return String(value) + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """); +} + +function renderAvmValue(value) { + if (Array.isArray(value)) { + return [ + "", + ...value.map((item) => ` ${escapeXml(item)}`), + "", + ].join("\n"); + } + + return escapeXml(value); +} + +function serializeFitsHeaderValue(value) { + if (typeof value === "string") { + return `'${value}'`; + } + + if (typeof value === "boolean") { + return value ? "T" : "F"; + } + + return `${value}`; +} + +function buildFitsHeaderFromWcs(wcs) { + const orderedKeys = [ + "NAXIS", + "NAXIS1", + "NAXIS2", + "CTYPE1", + "CTYPE2", + "CRPIX1", + "CRPIX2", + "CRVAL1", + "CRVAL2", + "CUNIT1", + "CUNIT2", + "CD1_1", + "CD1_2", + "CD2_1", + "CD2_2", + "PC1_1", + "PC1_2", + "PC2_1", + "PC2_2", + "CDELT1", + "CDELT2", + "CROTA2", + "LONPOLE", + "LATPOLE", + "EQUINOX", + "RADESYS", + ]; + + return orderedKeys + .filter((key) => wcs[key] !== undefined && wcs[key] !== null) + .map((key) => `${key.padEnd(8, " ")}= ${serializeFitsHeaderValue(wcs[key])}`) + .join("\n"); +} + +function getCoordinateFrameFromWcs(wcs) { + const ctype1 = `${wcs.CTYPE1 || ""}`.trim().toUpperCase(); + const ctype2 = `${wcs.CTYPE2 || ""}`.trim().toUpperCase(); + const radesys = `${wcs.RADESYS || ""}`.trim().toUpperCase(); + + if (ctype1.startsWith("GLON-") && ctype2.startsWith("GLAT-")) { + return "GAL"; + } + + if (ctype1.startsWith("RA---") && ctype2.startsWith("DEC--")) { + return radesys || "ICRS"; + } + + return null; +} + +function getProjectionFromWcs(wcs) { + const ctype1 = `${wcs.CTYPE1 || ""}`.trim().toUpperCase(); + const projection = ctype1.slice(-3); + + return /^[A-Z0-9]{3}$/.test(projection) ? projection : null; +} + +export function buildAvmFromWcs(wcs, options = {}) { + if (!wcs || typeof wcs !== "object") { + return null; + } + + const coordinateFrame = getCoordinateFrameFromWcs(wcs); + const projection = getProjectionFromWcs(wcs); + + if (!coordinateFrame || !projection) { + return null; + } + + const avm = { + "MetadataVersion": "1.2", + "Spatial.CoordinateFrame": coordinateFrame, + "Spatial.ReferenceValue": [wcs.CRVAL1, wcs.CRVAL2], + "Spatial.ReferenceDimension": [wcs.NAXIS1, wcs.NAXIS2], + "Spatial.ReferencePixel": [wcs.CRPIX1, wcs.CRPIX2], + "Spatial.CoordsystemProjection": projection, + "Spatial.FITSheader": buildFitsHeaderFromWcs(wcs), + }; + + if (wcs.EQUINOX !== undefined && wcs.EQUINOX !== null) { + avm["Spatial.Equinox"] = wcs.EQUINOX; + } + + if ( + wcs.CD1_1 !== undefined && + wcs.CD1_2 !== undefined && + wcs.CD2_1 !== undefined && + wcs.CD2_2 !== undefined + ) { + avm["Spatial.CDMatrix"] = [wcs.CD1_1, wcs.CD1_2, wcs.CD2_1, wcs.CD2_2]; + } else if (wcs.CDELT1 !== undefined && wcs.CDELT2 !== undefined) { + avm["Spatial.Scale"] = [wcs.CDELT1, wcs.CDELT2]; + + const rotation = options.rotation ?? wcs.CROTA2; + if (rotation !== undefined && rotation !== null) { + avm["Spatial.Rotation"] = rotation; + } + } + + return avm; +} + +export function buildAvmXmpPacket(avm) { + const entries = Object.entries(avm) + .map(([key, value]) => ` ${renderAvmValue(value)}`) + .join("\n"); + + return [ + ``, + '', + ' ', + ' ', + entries, + ' ', + ' ', + '', + '', + ].join("\n"); +} + +function makePngChunk(type4, data) { + const encoder = new TextEncoder(); + const type = encoder.encode(type4); + + if (type.length !== 4) { + throw new Error("PNG chunk type must be 4 bytes"); + } + + const crcInput = concatUint8Arrays([type, data]); + const crc = crc32(crcInput); + + return concatUint8Arrays([ + u32be(data.length), + type, + data, + u32be(crc), + ]); +} + +function makeXmpITXtChunk(xmpString) { + const encoder = new TextEncoder(); + + const keyword = encoder.encode("XML:com.adobe.xmp"); + const compressionFlag = new Uint8Array([0]); + const compressionMethod = new Uint8Array([0]); + const languageTag = new Uint8Array([0]); + const translatedKeyword = new Uint8Array([0]); + const text = encoder.encode(xmpString); + + const data = concatUint8Arrays([ + keyword, + new Uint8Array([0]), + compressionFlag, + compressionMethod, + languageTag, + translatedKeyword, + text, + ]); + + return makePngChunk("iTXt", data); +} + +export function insertXmpIntoPng(pngBytes, xmpString) { + if (!(pngBytes instanceof Uint8Array)) { + throw new Error("pngBytes must be a Uint8Array"); + } + + const pngSignature = new Uint8Array([ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, + ]); + + for (let i = 0; i < 8; i++) { + if (pngBytes[i] !== pngSignature[i]) { + throw new Error("Not a valid PNG"); + } + } + + const xmpChunk = makeXmpITXtChunk(xmpString); + let pos = 8; + let insertPos = -1; + + while (pos + 12 <= pngBytes.length) { + const length = + (pngBytes[pos] << 24) | + (pngBytes[pos + 1] << 16) | + (pngBytes[pos + 2] << 8) | + pngBytes[pos + 3]; + + const type = String.fromCharCode( + pngBytes[pos + 4], + pngBytes[pos + 5], + pngBytes[pos + 6], + pngBytes[pos + 7], + ); + + const chunkEnd = pos + 12 + length; + if (chunkEnd > pngBytes.length) { + throw new Error("Corrupt PNG"); + } + + if (type === "IDAT" || type === "IEND") { + insertPos = pos; + break; + } + + pos = chunkEnd; + } + + if (insertPos === -1) { + throw new Error("Could not find insertion point in PNG"); + } + + return concatUint8Arrays([ + pngBytes.subarray(0, insertPos), + xmpChunk, + pngBytes.subarray(insertPos), + ]); +} + +export async function injectAvmIntoPngBlob(blob, avm) { + const pngBytes = new Uint8Array(await blob.arrayBuffer()); + const xmp = buildAvmXmpPacket(avm); + const outBytes = insertXmpIntoPng(pngBytes, xmp); + + return new Blob([outBytes], { type: "image/png" }); +} + +export function blobToDataURL(blob) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onloadend = () => resolve(reader.result); + reader.onerror = () => reject(new Error("Error reading blob as data URL")); + reader.readAsDataURL(blob); + }); +} + +export function blobToArrayBuffer(blob) { + return blob.arrayBuffer(); +} diff --git a/src/js/View.js b/src/js/View.js index 1b404872..5fac86fb 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -50,8 +50,55 @@ import { Color } from "./Color.js"; import { SpectraDisplayer } from "./SpectraDisplayer.js"; import { DefaultActionsForContextMenu } from "./DefaultActionsForContextMenu.js"; import { Source } from "./Source.js"; +import { blobToArrayBuffer, blobToDataURL, buildAvmFromWcs, injectAvmIntoPngBlob } from "./AvmUtils.js"; export let View = (function () { + const normalizeImageType = function (imgType) { + return imgType || "image/png"; + }; + + const canvasToBlob = function (canvas, imgType) { + return new Promise((resolve, reject) => { + canvas.toBlob((blob) => { + if (blob) { + resolve(blob); + } else { + reject(new Error("Canvas toBlob failed")); + } + }, imgType); + }); + }; + + const exportCanvasBlob = async function (view, canvas, imgType) { + const effectiveImgType = normalizeImageType(imgType); + let blob = await canvasToBlob(canvas, effectiveImgType); + + if (effectiveImgType !== "image/png") { + return blob; + } + + const wcs = view.aladin.getViewWCS(); + if (!wcs || typeof wcs !== "object") { + return blob; + } + + const avm = buildAvmFromWcs(wcs, { + rotation: view.aladin.getRotation(), + }); + + if (!avm) { + return blob; + } + + try { + blob = await injectAvmIntoPngBlob(blob, avm); + } catch (error) { + console.warn("Could not inject AVM metadata into PNG export", error); + } + + return blob; + }; + /** Constructor */ function View(aladin) { this.aladin = aladin; @@ -562,20 +609,16 @@ export let View = (function () { */ View.prototype.getCanvasDataURL = async function (imgType, width, height, withLogo=true) { const c = await this.getCanvas(width, height, withLogo); - return c.toDataURL(imgType); + const blob = await exportCanvasBlob(this, c, imgType); + return blobToDataURL(blob); }; /** * Return ArrayBuffer corresponding to the current view */ View.prototype.getCanvasArrayBuffer = async function (imgType, width, height, withLogo=true) { - return this.getCanvasBlob(imgType, width, height, withLogo) - .then((blob) => { - const reader = new FileReader(); - reader.onloadend = () => resolve(reader.result); - reader.onerror = () => reject(new Error('Error reading blob as ArrayBuffer')); - reader.readAsArrayBuffer(blob); - }); + const blob = await this.getCanvasBlob(imgType, width, height, withLogo); + return blobToArrayBuffer(blob); } /** @@ -583,15 +626,7 @@ export let View = (function () { */ View.prototype.getCanvasBlob = async function (imgType, width, height, withLogo=true) { const c = await this.getCanvas(width, height, withLogo); - return new Promise((resolve, reject) => { - c.toBlob(blob => { - if (blob) { - resolve(blob); - } else { - reject(new Error('Canvas toBlob failed')); - } - }, imgType); - }); + return exportCanvasBlob(this, c, imgType); } View.prototype.selectLayer = function (layer) { From 44fdfad61c5b30eaa55f34479148eb83397a667f Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Wed, 27 May 2026 16:29:30 +0200 Subject: [PATCH 07/17] cargo clippy + fmt --- src/core/al-api/src/hips.rs | 3 - src/core/src/app.rs | 5 +- src/core/src/camera/viewport.rs | 3 +- src/core/src/renderable/image/mod.rs | 4 +- src/core/src/shaders.rs | 11102 ++++++++++++------------- 5 files changed, 5557 insertions(+), 5560 deletions(-) diff --git a/src/core/al-api/src/hips.rs b/src/core/al-api/src/hips.rs index a8e1ec2b..e2e58440 100644 --- a/src/core/al-api/src/hips.rs +++ b/src/core/al-api/src/hips.rs @@ -1,5 +1,3 @@ -use wasm_bindgen::JsValue; - use super::blend::BlendCfg; use serde::Deserialize; @@ -282,7 +280,6 @@ pub struct ImageMetadata { fn default_opacity() -> f32 { 1.0 } -use crate::Abort; impl ImageMetadata { pub fn visible(&self) -> bool { diff --git a/src/core/src/app.rs b/src/core/src/app.rs index 236bb713..0e27f66e 100644 --- a/src/core/src/app.rs +++ b/src/core/src/app.rs @@ -1854,7 +1854,10 @@ impl App { } pub(crate) fn get_fov(&self) -> [f64; 2] { - [self.camera.get_aperture().to_degrees(), self.camera.get_aperture_y().to_degrees()] + [ + self.camera.get_aperture().to_degrees(), + self.camera.get_aperture_y().to_degrees(), + ] } pub(crate) fn get_colormaps(&self) -> &Colormaps { diff --git a/src/core/src/camera/viewport.rs b/src/core/src/camera/viewport.rs index 78723b9f..e70be682 100644 --- a/src/core/src/camera/viewport.rs +++ b/src/core/src/camera/viewport.rs @@ -199,7 +199,7 @@ impl CameraViewPort { max_fov: None, scissor_w, - scissor_h + scissor_h, } } @@ -301,7 +301,6 @@ impl CameraViewPort { pub fn set_screen_size(&mut self, width: f32, height: f32, projection: &ProjectionType) { let old_w = self.width; - let old_h = self.height; self.width = width * self.dpi; self.height = height * self.dpi; diff --git a/src/core/src/renderable/image/mod.rs b/src/core/src/renderable/image/mod.rs index fbd3b330..97c27b0d 100644 --- a/src/core/src/renderable/image/mod.rs +++ b/src/core/src/renderable/image/mod.rs @@ -722,9 +722,7 @@ impl Image { //self.gl.enable(WebGl2RenderingContext::BLEND); let ImageMetadata { - opacity, - blending, - .. + opacity, blending, .. } = cfg; let shader = match self.pixel_type { diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index e8b954ae..1435500b 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -2,1963 +2,6 @@ use std::collections::HashMap; #[allow(dead_code)] pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"catalogs_mollweide.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mollweide(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.frag", - r#"#version 300 es -precision lowp float; -precision mediump int; - -out vec4 out_frag_color; - -uniform vec3 color; - -void main() { - out_frag_color = vec4(color, 1.0); -}"#, - ); - out.insert( - r"catalogs_healpix.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_healpix(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); -}"#, - ); - out.insert( - r"hips_rasterizer_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i16(uv0); - vec4 color_end = uvw2c_i16(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; - -out vec3 frag_uv; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; -}"#, - ); - out.insert( - r"hips_raytracer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; - -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i32(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_aitoff.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_aitoff(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"colormaps_colormap.vert", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -layout (location = 0) in vec2 position; -layout (location = 1) in vec2 uv; - -out vec2 out_uv; - -void main() { - gl_Position = vec4(position, 0.f, 1.f); - out_uv = uv; -}"#, - ); - out.insert( - r"fits_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i16(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_f32.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_f32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"hips_raytracer_raytracer.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; -layout (location = 1) in vec3 pos_world_space; - -out vec2 out_clip_pos; -out vec3 frag_pos; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform mat3 model; - -void main() { - vec2 uv = pos_clip_space * 0.5 + 0.5; - - frag_pos = model * pos_world_space; - - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); - out_clip_pos = pos_clip_space; -}"#, - ); out.insert( r"moc_base.frag", r#"#version 300 es @@ -1970,361 +13,6 @@ uniform vec4 u_color; void main() { color = u_color; -}"#, - ); - out.insert( - r"catalogs_catalog.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"fits_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -2355,600 +43,29 @@ void main() { }"#, ); out.insert( - r"catalogs_mercator.vert", + r"hips_raytracer_backcolor.vert", r#"#version 300 es precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; +precision mediump int; -uniform float current_time; -uniform mat3 inv_model; +layout (location = 0) in vec2 pos_clip_space; uniform vec2 ndc_to_clip; uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mercator(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); }"#, ); out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision mediump float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - -}"#, - ); - out.insert( - r"image_base.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"line_inst_lonlat.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; -layout (location = 2) in vec2 vertex; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -out vec2 l; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); - - vec2 da = p_a_clip - p_b_clip; - - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); -}"#, - ); - out.insert( - r"hips3d_f32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_f32(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_u8.frag", + r"hips_raytracer_rgba2cmap.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; -precision lowp usampler2DArray; +precision lowp sampler2DArray; precision lowp isampler2DArray; precision mediump int; -uniform sampler2DArray tex; - in vec3 frag_pos; in vec2 out_clip_pos; out vec4 out_frag_color; @@ -2963,6 +80,7 @@ struct Tile { uniform Tile textures_tiles[12]; uniform float opacity; +uniform sampler2DArray tex; uniform float scale; uniform float offset; @@ -3326,133 +444,35 @@ vec3 xyz2uv(vec3 xyz) { void main() { vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_u8(uv); - + vec4 c = uvw2cmap_rgba(uv); out_frag_color = c; out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"moc_base.vert", + r"line_inst_ndc.vert", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 lonlat; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; +layout (location = 2) in vec2 vertex; -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; +out vec2 l; -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} +uniform float u_width; +uniform float u_height; +uniform float u_thickness; void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); + vec2 x_b = p_b - p_a; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); + float ndc2pix = 2.0 / u_width; + + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); }"#, ); out.insert( @@ -3851,1602 +871,22 @@ void main() { }"#, ); out.insert( - r"hips3d_u8.frag", + r"catalogs_ortho.vert", r#"#version 300 es precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_u8(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"image_sampler.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -void main() { - out_frag_color = texture(tex, frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_red.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = vec3(frag_uv.xyz); - vec4 color = uvw2c_ra(uv); - - out_frag_color = color; - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i32(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i32(uv0); - vec4 color_end = uvw2c_i32(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_base.vert", - r#"#version 300 es -precision highp float; -precision highp int; - -layout (location = 0) in vec2 ndc_pos; +layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"line_inst_ndc.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; -layout (location = 2) in vec2 vertex; - -out vec2 l; - -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -void main() { - vec2 x_b = p_b - p_a; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); -}"#, - ); - out.insert( - r"passes_post_vertex_100es.vert", - r#"#version 300 es -precision mediump float; - -layout (location = 0) in vec2 a_pos; -out vec2 v_tc; - -void main() { - gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); - v_tc = a_pos; -}"#, - ); - out.insert( - r"colormaps_colormap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -in vec2 out_uv; -out vec4 color; - -uniform sampler2D texture_fbo; -uniform float alpha; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} - -void main() { - float opacity = texture(texture_fbo, out_uv).r; - - float o = smoothstep(0.0, 0.1, opacity); - - color = colormap_f(opacity); - color.a = o * alpha; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; - -uniform vec2 ndc_to_clip; -uniform float czf; - -void main() { - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; const float PI = 3.141592653589793; const float SQRT_2 = 1.41421356237309504880168872420969808; @@ -5549,886 +989,163 @@ vec2 proj(vec3 p) { } } + void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); + vec3 p = inv_model * center; - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); + vec2 center_pos_clip_space = world2clip_orthographic(p); - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; }"#, ); out.insert( - r"fits_u8.frag", + r"hips_raytracer_backcolor.frag", r#"#version 300 es precision lowp float; -precision lowp sampler2D; precision mediump int; out vec4 out_frag_color; -in vec2 frag_uv; -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} +uniform vec3 color; void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_u8(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; + out_frag_color = vec4(color, 1.0); }"#, ); out.insert( - r"hips_rasterizer_rgba2cmap.frag", + r"catalogs_mercator.vert", r#"#version 300 es precision lowp float; -precision lowp sampler2DArray; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; -uniform sampler2DArray tex; +uniform float current_time; +uniform mat3 inv_model; -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; -out vec4 out_frag_color; -uniform float opacity; +out vec2 out_uv; +out vec3 out_p; -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2cmap_rgba(frag_uv_start); - vec4 color_end = uvw2cmap_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"hips_raytracer_f32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -struct TileColor { - Tile tile; - vec4 color; - bool found; -}; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; +const float SQRT_2 = 1.41421356237309504880168872420969808; -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); } -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; } else { - return x02 - 1.0; + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; } + + return vec2(x2d * 0.5, y2d) / SQRT_2; } +const float eps = 1.25e-8; +const int n_iter = 100; -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; } - return 1.0f - p.z; + + return 0.5 * x; } -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; } -int ij2z(int i, int j) { - int i4 = i | (j << 2); +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); - return i5; + return vec3(dc * ts, ds, dc * tc); } -struct HashDxDy { - int idx; - float dx; - float dy; -}; +uniform int u_proj; -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + return w2c_mer(p); } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); } -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); + vec3 p = inv_model * center; - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_f32(uv); + vec2 center_pos_clip_space = world2clip_mercator(p); + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; + out_uv = uv; + out_p = p; }"#, ); out.insert( @@ -6971,7 +1688,603 @@ void main() { }"#, ); out.insert( - r"catalogs_ortho.vert", + r"hips3d_raster.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; + +out vec3 frag_uv; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv = uv; +}"#, + ); + out.insert( + r"fits_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_u8(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"catalogs_arc.vert", r#"#version 300 es precision lowp float; layout (location = 0) in vec2 offset; @@ -7089,17 +2402,1658 @@ vec2 proj(vec3 p) { } } - void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_orthographic(p); + vec2 center_pos_clip_space = world2clip_arc(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); out_uv = uv; out_p = p; +}"#, + ); + out.insert( + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); +}"#, + ); + out.insert( + r"fits_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i16(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"moc_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 lonlat; + +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.f, 1.f); +}"#, + ); + out.insert( + r"hips3d_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_u8(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); + out_uv = uv; +}"#, + ); + out.insert( + r"colormaps_colormap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +in vec2 out_uv; +out vec4 color; + +uniform sampler2D texture_fbo; +uniform float alpha; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} + +void main() { + float opacity = texture(texture_fbo, out_uv).r; + + float o = smoothstep(0.0, 0.1, opacity); + + color = colormap_f(opacity); + color.a = o * alpha; +}"#, + ); + out.insert( + r"hips_rasterizer_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i32(uv0); + vec4 color_end = uvw2c_i32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp usampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_u8(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_f32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -7374,136 +4328,18 @@ void main() { }"#, ); out.insert( - r"catalogs_tan.vert", + r"image_base.vert", r#"#version 300 es -precision lowp float; +precision highp float; -layout (location = 0) in vec2 offset; +layout (location = 0) in vec2 ndc_pos; layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} +out vec2 frag_uv; void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_gnomonic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; }"#, ); out.insert( @@ -7897,6 +4733,3170 @@ void main() { out_frag_color = c; out_frag_color = vec4(c.rgb, opacity * c.a); +}"#, + ); + out.insert( + r"catalogs_ortho.frag", + r#"#version 300 es +precision lowp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"catalogs_healpix.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_healpix(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips3d_f32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_f32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"passes_post_fragment_100es.frag", + r#"#version 300 es +precision mediump float; + +in vec2 v_tc; +out vec4 color; + +uniform sampler2D fbo_tex; + +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); +} + +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); +} + +void main() { + color = texture(fbo_tex, v_tc); + +}"#, + ); + out.insert( + r"hips3d_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_rasterizer_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i16(uv0); + vec4 color_end = uvw2c_i16(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"line_inst_lonlat.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; +layout (location = 2) in vec2 vertex; + +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; + +out vec2 l; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); + + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); +}"#, + ); + out.insert( + r"hips_raytracer_raytracer.vert", + r#"#version 300 es +precision lowp float; +precision mediump int; + +layout (location = 0) in vec2 pos_clip_space; +layout (location = 1) in vec3 pos_world_space; + +out vec2 out_clip_pos; +out vec3 frag_pos; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform mat3 model; + +void main() { + vec2 uv = pos_clip_space * 0.5 + 0.5; + + frag_pos = model * pos_world_space; + + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); + out_clip_pos = pos_clip_space; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba2cmap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec4 color_start = uvw2cmap_rgba(frag_uv_start); + vec4 color_end = uvw2cmap_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"catalogs_catalog.frag", + r#"#version 300 es +precision lowp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"passes_post_vertex_100es.vert", + r#"#version 300 es +precision mediump float; + +layout (location = 0) in vec2 a_pos; +out vec2 v_tc; + +void main() { + gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); + v_tc = a_pos; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"image_sampler.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +void main() { + out_frag_color = texture(tex, frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_base.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"hips_raytracer_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp usampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i32(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_red.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec3 uv = vec3(frag_uv.xyz); + vec4 color = uvw2c_ra(uv); + + out_frag_color = color; + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips_raytracer_f32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision mediump int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +struct TileColor { + Tile tile; + vec4 color; + bool found; +}; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_f32(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_tan.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_gnomonic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_aitoff.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_aitoff(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; }"#, ); out From 6ce7e9365da6f60cffdd0bd0eaa7fc880af7b3b8 Mon Sep 17 00:00:00 2001 From: Tom Donaldson Date: Fri, 8 May 2026 11:47:56 -0400 Subject: [PATCH 08/17] Squash all skewer_selection commits to date --- assets/icons/edge_selection-arrow.svg | 7 + assets/icons/edge_selection.svg | 6 + assets/icons/skewer_selection-arrow.svg | 8 + assets/icons/skewer_selection_black.svg | 7 + src/js/Aladin.js | 16 +- src/js/Selector.js | 67 ++++- src/js/View.js | 343 +++++++++++++++++++----- src/js/gui/Button/SelectionMode.js | 149 ++++++++++ tutorials/UI.md | 1 + 9 files changed, 530 insertions(+), 74 deletions(-) create mode 100644 assets/icons/edge_selection-arrow.svg create mode 100644 assets/icons/edge_selection.svg create mode 100644 assets/icons/skewer_selection-arrow.svg create mode 100644 assets/icons/skewer_selection_black.svg create mode 100644 src/js/gui/Button/SelectionMode.js diff --git a/assets/icons/edge_selection-arrow.svg b/assets/icons/edge_selection-arrow.svg new file mode 100644 index 00000000..e4c4aafd --- /dev/null +++ b/assets/icons/edge_selection-arrow.svg @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/assets/icons/edge_selection.svg b/assets/icons/edge_selection.svg new file mode 100644 index 00000000..e5285e63 --- /dev/null +++ b/assets/icons/edge_selection.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/assets/icons/skewer_selection-arrow.svg b/assets/icons/skewer_selection-arrow.svg new file mode 100644 index 00000000..75695d0c --- /dev/null +++ b/assets/icons/skewer_selection-arrow.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/assets/icons/skewer_selection_black.svg b/assets/icons/skewer_selection_black.svg new file mode 100644 index 00000000..9cc1d200 --- /dev/null +++ b/assets/icons/skewer_selection_black.svg @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/js/Aladin.js b/src/js/Aladin.js index 853f9335..c9d88500 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -74,6 +74,7 @@ import { SimbadPointer } from "./gui/Button/SimbadPointer"; import { ColorPicker } from "./gui/Button/ColorPicker"; import { GridEnabler } from "./gui/Button/GridEnabler"; import { CooFrame } from "./gui/Input/CooFrame"; +import { SelectionMode } from "./gui/Button/SelectionMode"; import { Circle } from "./shapes/Circle"; import { Ellipse } from "./shapes/Ellipse"; import { Polyline } from "./shapes/Polyline"; @@ -110,6 +111,8 @@ import { Polyline } from "./shapes/Polyline"; * CSS class for that button is `aladin-grid-control` * @property {boolean} [showSettingsControl=false] - Whether to show the settings control toolbar. * CSS class for that button is `aladin-settings-control` + * @property {boolean} [showSelectionModeControl=false] - Whether to show the selection mode menu opener button. + * CSS class for that button is `aladin-selectionMode-control` * @property {boolean} [showColorPickerControl=false] - Whether to show the color picker tool. * CSS class for that button is `aladin-colorPicker-control` * @property {boolean} [showShareControl=false] - Whether to show the share control toolbar. @@ -155,6 +158,7 @@ import { Polyline } from "./shapes/Polyline"; * @property {boolean} [pixelateCanvas=true] - Whether to pixelate the canvas. * @property {boolean} [manualSelection=false] - When set to true, no selection will be performed, only events will be generated. * @property {string} [mode] - Interface theme, can be either 'dark' or 'light'. If not set, the mode will be retrieved from your browser preference or your localStorage. + * @property {string} [selectionMode="edge"] - When set to 'skewer' footprints are selected by clicking inside them. For 'edge' footprints are selected by clicking on their edges. * @property {Object} [selector] - More options for the the selector. * @property {string} [selector.color] - Color of the selector, defaults to the color of the reticle. Can be a hex color or a function returning a hex color. * @property {number} [selector.lineWidth=2] - Width of the selector line. @@ -674,15 +678,19 @@ export let Aladin = (function () { widgets["simbad"] = simbad } - // Add the projection control // Add the coo grid control if (options.showCooGridControl) { let grid = new GridEnabler(this); widgets["grid"] = grid; } - // Add the projection control - // Add the coo grid control + // Show selection mode control + if (options.showSelectionModeControl) { + let selectionMode = new SelectionMode(this); + widgets["selectionMode"] = selectionMode + } + + // Add the color picker control if (options.showColorPickerControl) { let picker = new ColorPicker(this); widgets["picker"] = picker; @@ -698,6 +706,7 @@ export let Aladin = (function () { this.toolbar.add(name, widget); } + // Add the projection control if (options.showProjectionControl) { this.projBtn = new ProjectionActionButton(this); this.addUI(this.projBtn); @@ -784,6 +793,7 @@ export let Aladin = (function () { showSimbadPointerControl: false, showCooGridControl: false, showSettingsControl: false, + showSelectionModeControl: false, showColorPickerControl: false, // Share toolbar showShareControl: false, diff --git a/src/js/Selector.js b/src/js/Selector.js index bd46dbb9..cfac364d 100644 --- a/src/js/Selector.js +++ b/src/js/Selector.js @@ -27,15 +27,16 @@ import { PolySelect } from "./FiniteStateMachine/PolySelect"; import { LineSelect } from "./FiniteStateMachine/LineSelect"; import { RectSelect } from "./FiniteStateMachine/RectSelect"; import { ALEvent } from "./events/ALEvent"; +import { Utils } from './Utils'; /****************************************************************************** * Aladin Lite project - * + * * Class Selector - * + * * A selector - * + * * Author: Matthieu Baumann[CDS] - * + * *****************************************************************************/ export class Selector { @@ -121,7 +122,7 @@ export class Selector { continue; } sources = cat.getSources(); - + for (var l = 0; l < sources.length; l++) { s = sources[l]; @@ -172,4 +173,60 @@ export class Selector { return objList; } + + /** + * Retrieves objects skewered by the cursor position or specified coordinates. An object is + * skewered if it is a shape that contains the specified coordinate, or is a catalog object within 3 pixels + * of the specified coordinate. + * + * If e is a mouse event (as opposed to an object with x and y values), the mouse coordinates + * of the event are used. + * + * This is implemented by simulating the interactive selection of a circle region with a 3 pixel radius) + * around the given coordinates and returns all catalog sources and overlay items intersecting with it. + * + * @param {Event|Object} e - Mouse coordinate via mouse event or object with x and y properties + * @param {Object} view - The Aladin View instance containing catalogs and overlays + * @returns {Array} Array of object lists, where each subarray contains objects + * from a single catalog or overlay that intersect with the selection region. + * Returns empty array if no objects are found. + */ + static getSkewerObjects(e, view) { + // Get the xy from the event + let xymouse; + if (e instanceof Event) { + xymouse = Utils.relMouseCoords(e); + } else { + xymouse = e; + } + const x = xymouse.x; + const y = xymouse.y; + + // Perform a selection using a circle around x, y as if drawn by dragging 3 pixels. + const r2 = 9; + const r = Math.sqrt(r2); + + let selectorObject = { + x, y, r, + label: 'circle', + contains(s) { + let dx = (s.x - x) + let dy = (s.y - y); + + return dx*dx + dy*dy <= r2; + }, + bbox() { + return { + x: x - r, + y: y - r, + w: 2*r, + h: 2*r + } + } + }; + + let objList = Selector.getObjects(selectorObject, view); + + return objList; + } } \ No newline at end of file diff --git a/src/js/View.js b/src/js/View.js index 5fac86fb..83c7e664 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -273,6 +273,13 @@ export let View = (function () { this.selector = new Selector(this, this.options.selector); this.manualSelection = (this.options && this.options.manualSelection) || false; + // Selection mode + this.selectionMode = View.SELECTION_MODE_EDGE; + if (this.options.selectionMode === 'skewer') { + this.selectionMode = View.SELECTION_MODE_SKEWER; + } + + // current reference image survey displayed this.imageLayers = new Map(); @@ -359,6 +366,10 @@ export let View = (function () { View.TOOL_SIMBAD_POINTER = 2; View.TOOL_COLOR_PICKER = 3; + // Selection modes + View.SELECTION_MODE_EDGE = 0; + View.SELECTION_MODE_SKEWER = 1; + // TODO: should be put as an option at layer level View.DRAW_SOURCES_WHILE_DRAGGING = true; View.DRAW_MOCS_WHILE_DRAGGING = true; @@ -514,6 +525,13 @@ export let View = (function () { } View.prototype.setMode = function (mode, params) { + + // Undo the specialized cursors, if any. + const prevMode = this.mode; + if (prevMode == View.TOOL_SIMBAD_POINTER) { + this.catalogCanvas.classList.remove('aladin-sp-cursor'); + } + // hide the picker tooltip this.colorPickerTool.domElement.style.display = "none"; // in case we are in the selection mode @@ -546,6 +564,14 @@ export let View = (function () { ALEvent.MODE.dispatchedTo(this.aladin.aladinDiv, {mode}); }; + View.prototype.setSelectionMode = function (selectionMode) { + this.selectionMode = selectionMode; + }; + + View.prototype.getSelectionMode = function () { + return this.selectionMode; + }; + View.prototype.setCursor = function (cursor) { if (this.catalogCanvas.style.cursor == cursor) { return; @@ -713,12 +739,10 @@ export let View = (function () { var showContextMenu = true; var xystart; - var handleSelect = function(xy, tolerance) { + var handleSelect = function(xy, tolerance, withModifierKey=false) { tolerance = tolerance || 5; var objs = view.closestObjects(xy.x, xy.y, tolerance); - view.unselectObjects(); - if (objs) { var objClickedFunction = view.aladin.callbacksByEventName['objectClicked']; var footprintClickedFunction = view.aladin.callbacksByEventName['footprintClicked']; @@ -757,11 +781,14 @@ export let View = (function () { if (shapes.length > 0) { objs.push(shapes) } - view.selectObjects(objs); + view.selectObjects(objs, withModifierKey); view.lastClickedObject = objs; } else { + + view.unselectObjects(); + // If there is a past clicked object if (view.lastClickedObject) { // TODO: do we need to keep that triggering ? @@ -772,6 +799,95 @@ export let View = (function () { } } } + + /** + * Perform a skewer-based selection of objects at the specified mouse position. + * + * @param {Event|Object} e - Mouse coordinate via mouse event or object with x and y properties + * @param {boolean} withModifierKey - If true, toggles selection (adds/removes from existing selection); + * if false, replaces current selection + */ + var handleSkewerSelect = function(e, withModifierKey) { + + const objList = Selector.getSkewerObjects(e, view); + view.selectObjects(objList, withModifierKey); + } + + /** + * Make the selected objects appear hovered and make all other objects appear unhovered, + * firing the appropriate callbacks for both cases. + * + * The also sets the cursor to a pointer to indicate that some object(s) would be + * select on click in the current mouse position. + * + * @param {Array} objects - Array of objects to apply hover state to + * @param {Object} xymouse - Mouse coordinates with x and y properties + */ + var hoverObjects = function(objects, xymouse) { + var objHoveredFunction = view.aladin.callbacksByEventName['objectHovered']; + var footprintHoveredFunction = view.aladin.callbacksByEventName['footprintHovered']; + + view.setCursor('pointer'); + + for (let o of objects) { + + if (typeof objHoveredFunction === 'function' && (!view.lastHoveredObject || !view.lastHoveredObject.includes(o))) { + var ret = objHoveredFunction(o, xymouse); + } + + if (o.isFootprint()) { + if (typeof footprintHoveredFunction === 'function' && (!view.lastHoveredObject || !view.lastHoveredObject.includes(o))) { + var ret = footprintHoveredFunction(o, xymouse); + } + } + + if (!view.lastHoveredObject || !view.lastHoveredObject.includes(o)) { + o.hover(); + } + } + + // unhover the objects in lastHoveredObjects that are not in closest anymore + if (view.lastHoveredObject) { + var objHoveredStopFunction = view.aladin.callbacksByEventName['objectHoveredStop']; + + for (let lho of view.lastHoveredObject) { + if (!objects.includes(lho)) { + lho.unhover(); + + if (typeof objHoveredStopFunction === 'function') { + objHoveredStopFunction(lho, xymouse); + } + } + } + } + view.lastHoveredObject = objects; + } + + /** + * Removes hover state from all previously hovered objects and fires the + * apropriate callbacks. + * + * The also resets the cursor to the default to indicate that no objects would be + * selected on click in the current mouse position. + * + * @param {Object} xymouse - Mouse coordinates with x and y properties + */ + var unhoverObjects = function(xymouse) { + view.setCursor('default'); + if (view.lastHoveredObject) { + var objHoveredStopFunction = view.aladin.callbacksByEventName['objectHoveredStop']; + for (let lho of view.lastHoveredObject) { + lho.unhover(); + + if (typeof objHoveredStopFunction === 'function') { + objHoveredStopFunction(lho, xymouse); + } + } + } + + view.lastHoveredObject = null; + } + var touchStartTime; Utils.on(view.catalogCanvas, "mousedown touchstart", function (e) { e.stopPropagation(); @@ -918,6 +1034,7 @@ export let View = (function () { // reacting on 'click' rather on 'mouseup' is more reliable when panning the view Utils.on(view.catalogCanvas, "mouseup mouseout touchend touchcancel", function (e) { const xymouse = Utils.relMouseCoords(e); + const withModifierKey = e.ctrlKey || e.metaKey; ALEvent.CANVAS_EVENT.dispatchedTo(view.aladinDiv, { state: { @@ -1006,11 +1123,19 @@ export let View = (function () { const elapsedTime = Date.now() - touchStartTime; if (elapsedTime < 100) { view.updateObjectsLookup(); - handleSelect(xymouse, 15); + if (view.selectionMode === View.SELECTION_MODE_SKEWER) { + handleSkewerSelect(e, withModifierKey) + } else { + handleSelect(xymouse, 15, withModifierKey); + } } } } else { - handleSelect(xymouse); + if (view.selectionMode === View.SELECTION_MODE_EDGE) { + handleSelect(xymouse, 5, withModifierKey); + } else { + handleSkewerSelect(e, withModifierKey); + } } } @@ -1193,71 +1318,31 @@ export let View = (function () { lastMouseMovePos = pos; } - // closestObjects is very costly, we would like to not do it - // especially if the objectHovered function is not defined. - var closests = view.closestObjects(xymouse.x, xymouse.y, 5); + if (view.selectionMode === View.SELECTION_MODE_EDGE) { + // We're in edge selection mode for footprints. closestObjects() will find those footprints by closeness to a footprint edge. + // closestObjects is very costly, we would like to not do it + // especially if the objectHovered function is not defined. + var closests = view.closestObjects(xymouse.x, xymouse.y, 5); - if (closests) { - var objHoveredFunction = view.aladin.callbacksByEventName['objectHovered']; - var footprintHoveredFunction = view.aladin.callbacksByEventName['footprintHovered']; + if (closests) { + hoverObjects(closests, xymouse); + } else { + unhoverObjects(view, xymouse); + } + } else if (view.selectionMode === View.SELECTION_MODE_SKEWER) { + // We're in skewer mode. Let's see what would be selected. + const skewerTargetsByLayer = Selector.getSkewerObjects(e, view); + const skewerObjects = skewerTargetsByLayer.flat(); - view.setCursor('pointer'); - - for (let o of closests) { - - if (typeof objHoveredFunction === 'function' && (!view.lastHoveredObject || !view.lastHoveredObject.includes(o))) { - var ret = objHoveredFunction(o, xymouse); - } - - if (o.isFootprint()) { - if (typeof footprintHoveredFunction === 'function' && (!view.lastHoveredObject || !view.lastHoveredObject.includes(o))) { - var ret = footprintHoveredFunction(o, xymouse); - } - } - - if (!view.lastHoveredObject || !view.lastHoveredObject.includes(o)) { - o.hover(); - } + if (skewerObjects.length > 0) { + hoverObjects(skewerObjects, xymouse); + } else { + unhoverObjects(view, xymouse); } - // unhover the objects in lastHoveredObjects that are not in closest anymore - if (view.lastHoveredObject) { - var objHoveredStopFunction = view.aladin.callbacksByEventName['objectHoveredStop']; - - for (let lho of view.lastHoveredObject) { - if (!closests.includes(lho)) { - lho.unhover(); - - if (typeof objHoveredStopFunction === 'function') { - objHoveredStopFunction(lho, xymouse); - } - } - } - } - view.lastHoveredObject = closests; - } else { - view.setCursor('default'); - if (view.lastHoveredObject) { - var objHoveredStopFunction = view.aladin.callbacksByEventName['objectHoveredStop']; - - /*if (typeof objHoveredStopFunction === 'function') { - // call callback function to notify we left the hovered object - var ret = objHoveredStopFunction(view.lastHoveredObject, xymouse); - } - - view.lastHoveredObject.unhover();*/ - for (let lho of view.lastHoveredObject) { - lho.unhover(); - - if (typeof objHoveredStopFunction === 'function') { - objHoveredStopFunction(lho, xymouse); - } - } - } - - view.lastHoveredObject = null; } + if (e.type === "mousemove") { return; } @@ -1391,6 +1476,7 @@ export let View = (function () { view.displayHpxGrid = false; view.displayCatalog = false; + view.skewerEnabled = false; }; View.prototype.requestRedrawAtDate = function (date) { @@ -1636,6 +1722,9 @@ export let View = (function () { return imageData; }; + /** + * Unselects all currently selected objects. + */ View.prototype.unselectObjects = function() { if (this.manualSelection) { return; @@ -1654,11 +1743,24 @@ export let View = (function () { this.requestRedraw(); } - View.prototype.selectObjects = function(selection) { + /** + * Selects the specified objects in the view. + * + * If withModifierKey is true, it modifies the existing selection (adds/removes). + * Otherwise, it replaces the current selection. + * + * @param {Array|Object} selection - The objects to select, either an array or a selector object. + * @param {boolean} [withModifierKey=false] - Whether to modify (versus replace) the existing selections. + */ + View.prototype.selectObjects = function(selection, withModifierKey=false) { if (this.manualSelection) { return; } + if (Array.isArray(selection) && withModifierKey) { + selection = this.computeModifiedSelection(selection) + } + // unselect the previous selection this.unselectObjects(); @@ -1736,6 +1838,113 @@ export let View = (function () { } } + View.prototype._getLayerForObj = function(obj) { + let layer = null; + if (obj.getCatalog) { + layer = obj.getCatalog() + } else { + layer = obj.overlay + } + return layer + } + + View.prototype._copySelectionsToStage = function(selections, stage, overlays, exclude) { + for (const group of selections) { + for (const obj of group) { + const objExcluded = exclude.includes(obj) + if (!objExcluded) { + const layer = this._getLayerForObj(obj) + const idx = overlays.findIndex(item => item.uuid === layer.uuid); + if (idx >= 0) { + stage[idx].push(obj) + } else { + console.warn("Layer not found for selected obj: " + obj) + } + } + } + } + } + + /** + * Computes the full set of selections that should result if the specified (pending) objects were + * selected with a modifier key pressed. + * + * If there are existing selections, it adds pending items that aren't selected, + * or removes all pending items if they are all already selected. + * + * Organizes selections by overlay layers as expected by selectObjects. + * + * @param {Array} pending - Array of array of objects to potentially add/remove from selection. + * @returns {Array} The modified selection array. + */ + View.prototype.computeModifiedSelection = function(pending) { + const current = this.selection + let modSelection = pending + if (current && current.length > 0) { + // There are some items already selected. + // We will be adding all the pending selections that are not already selected, + // UNLESS all of the pending selections are already selected, in which case + // they will all be unselected. + const toAdd = [] + let mightRemove = [] + modSelection = [] // We will build a new selection list from the current and pending selections + + // stage will have one row for each existing overlay in which to collect all desired selections. + const overlays = this.aladin.getOverlays() + const stage = new Array(overlays.length).fill(null).map(() => []); + + // Put already-selected items in mightRemove and not-yet-selected items in toAdd. + for (const group of pending) { + for (const obj of group) { + if (obj.isSelected) { + mightRemove.push(obj) + } else { + toAdd.push(obj) + } + } + } + + // If there is anything in toAdd, then clear mightRemove since they will be left selected. + if (toAdd.length > 0) { + mightRemove = [] + } + + // Copy current selections to stage except for anything in mightRemove + this._copySelectionsToStage(current, stage, overlays, mightRemove) + + // Copy toAdd selections to stage + this._copySelectionsToStage([toAdd], stage, overlays, []) + + // Build new modified selections list from stage. + // I can preserve the layer order, but I don't know how to preserve the order within + // layers without looping through all objects. Hopefully that order doesn't matter. + for (let i=0; i 0) { + // We have selected objects in this layer so will add the layer (or list of overlays) to modSelection + + if (overlays[i].type === 'catalog') { + // The layer is a catalog so we add one entry for all its selections + const catLayer = [] + modSelection.push(catLayer) + for (const obj of stage[i]) { + catLayer.push(obj) + } + + } else { + // Assume it's a graphicalOverlay and add separate entries for each selected obj + // (That is the way objects in graphicalOverlays are currently selected. If they start + // being selected all in one list per overlay, then that can change here.) + for (const obj of stage[i]) { + modSelection.push([obj]) + } + } + } + } + + } + return modSelection; + } + View.prototype.getVisibleCells = function (norder) { return this.wasm.getVisibleCells(norder); }; @@ -2429,3 +2638,5 @@ export let View = (function () { return View; })(); + + diff --git a/src/js/gui/Button/SelectionMode.js b/src/js/gui/Button/SelectionMode.js new file mode 100644 index 00000000..2b5ab0b6 --- /dev/null +++ b/src/js/gui/Button/SelectionMode.js @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2013 - UDS/CNRS +// The Aladin Lite program is distributed under the terms +// of the GNU Lesser General Public License version 3 +// or (at your option) any later version. +// +// This file is part of Aladin Lite. +// +// Aladin Lite is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Aladin Lite is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with Aladin Lite. If not, see . +// + +import { CtxMenuActionButtonOpener } from "./CtxMenuOpener"; +import skewerSelectionIconArrow from '../../../../assets/icons/skewer_selection-arrow.svg'; +import skewerSelectionIcon from '../../../../assets/icons/skewer_selection_black.svg'; +import edgeSelectionIconArrow from '../../../../assets/icons/edge_selection-arrow.svg'; +import edgeSelectionIcon from '../../../../assets/icons/edge_selection.svg'; +import { View } from "../../View.js"; + +/****************************************************************************** + * Aladin Lite project + * + * File gui/Button/SelectionMode.js + * + * Class representing a button for bringing up a menu for choosing selection mode. + * The appearance of the button changes depending on which selection mode (View.getSelectionMode()) + * is active. + * + * There are two possible selection modes, Edge and Skewer, which affect how footprints are + * interactively selected. + * + * In Edge mode (View.SELECTION_MODE_EDGE), footprints are selected by clicking on their edges. + * + * In Skewer mode (View.SELECTION_MODE_SKEWER), footprints are selecting by clicking anywhere + * inside the footprint. + * + * Using a modifier key (Cmd on Mac, Ctrl otherwise) during select toggles the potential selections: + * - If any of the potential selections are not already selected, those objects are added to the current selections. + * - If all of the potential selections are already selected, then they are deselected. + * + * This uses the CSS class aladin-selectionMode-control. + * + * Author: Tom Donaldson (STScI) + * + *****************************************************************************/ + export class SelectionMode extends CtxMenuActionButtonOpener { + /** + * Class representing a button for bringing up a menu for choosing selection mode. + * @param {Aladin} aladin - The aladin instance. + */ + constructor(aladin, options) { + + // If we're on Mac, the modifier key will be Cmd instead of Ctrl. + let modifierKey = 'Ctrl'; + const userAgent = window.navigator.userAgent.toLowerCase(); + if (userAgent.indexOf('mac') > -1) { + modifierKey = 'Cmd'; + } + + // Set the initial button icon based on the current View selection mode. + const initialMode = aladin.view.getSelectionMode(); + let initialIcon = edgeSelectionIconArrow; + if (initialMode === View.SELECTION_MODE_SKEWER) { + initialIcon = skewerSelectionIconArrow; + } + + super({ + icon: { + size: 'medium', + monochrome: true, + url: initialIcon, + }, + classList: ['aladin-selectionMode-control'], + tooltip: { + content: 'Choose the selection mode
    (' + modifierKey + ' for multiselect)', + position: { direction: 'top right', top: '10%', left: '80%' }, + }, + ctxMenu: undefined, + ...options + }, aladin); + + this.aladin = aladin; + this.modifierKey = modifierKey; + let ctxMenu = this._buildLayout() + this.update({ctxMenu}) + } + + setCustomIcon(icon) { + this.update({icon: { + size: 'medium', + monochrome: true, + url: icon + }}) + } + + _buildLayout() { + let self = this; + let aladin = this.aladin; + + return [ + { + label: { + icon: { + url: skewerSelectionIcon, + monochrome: true, + }, + tooltip: { + content: 'Click inside shapes to select.
    Multiselect with ' + self.modifierKey + '.', + position: { direction: 'top right', left: '50%' }, + }, + content: "Skewer Selection", + }, + action: (e) => { + aladin.view.setSelectionMode(View.SELECTION_MODE_SKEWER); + self.setCustomIcon(skewerSelectionIconArrow); + }, + }, + { + label: { + icon: { + url: edgeSelectionIcon, + monochrome: true, + }, + tooltip: { + content: 'Click on objects to select.
    Multiselect with ' + self.modifierKey + '.', + position: { direction: 'top right', left: '60%' }, + }, + content: "Edge Selection", + }, + action: (e) => { + aladin.view.setSelectionMode(View.SELECTION_MODE_EDGE); + self.setCustomIcon(edgeSelectionIconArrow); + }, + }, + ] + } + +} + diff --git a/tutorials/UI.md b/tutorials/UI.md index 00db33ed..66e20dcd 100644 --- a/tutorials/UI.md +++ b/tutorials/UI.md @@ -11,6 +11,7 @@ There are distincts CSS class names for users wanting to personnalize the defaul * `aladin-simbadPointer-control` targets the Simbad pointer control button * `aladin-grid-control` targets the coordinate grid trigger button * `aladin-settings-control` targets the settings menu opener button +* `aladin-selectionMode-control` targets the selection mode menu opener button * `aladin-share-control` targets the share menu opener button * `aladin-projection-control` targets the projection selector button * `aladin-stack-box` targets the stack box From 9392eddc36ffce35fa445d3e3966f90d90a386ef Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Wed, 27 May 2026 16:36:29 +0200 Subject: [PATCH 09/17] add changelog entry --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb155d21..73253010 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * [feat] Support of SIP astrometry keywords in FITS file * [fix] * [feat] Progressive catalogs: first try loading metadata.xml, switch back on Metadata.xml if it fails +* [feat] Support skewer selection for footprints/shapes by [@tomdonaldson][tomdonaldson] ### 3.8.0 @@ -541,6 +542,7 @@ Aladin Lite is made possible thanks to those people: * [@simontorres][simontorres] * [@szpetny][szpetny] * [@tboch][tboch] +* [@tomdonaldson][tomdonaldson] * [@Xen0Xys][Xen0Xys] [alexgoff]: https://github.com/alexgoff @@ -553,5 +555,5 @@ Aladin Lite is made possible thanks to those people: [simontorres]: https://github.com/simontorres [szpetny]: https://github.com/szpetny [tboch]: https://github.com/tboch +[tomdonaldson]: https://github.com/tomdonaldson [Xen0Xys]: https://github.com/Xen0Xys - From 9afb07daa91c2e46c3afc02bc33aa8297f9a747d Mon Sep 17 00:00:00 2001 From: Philip Matsson Date: Fri, 17 Apr 2026 10:04:58 +0200 Subject: [PATCH 10/17] Include hips_data_range in HiPS property parser --- src/js/HiPS.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/js/HiPS.js b/src/js/HiPS.js index 2a237727..5f1b9f7d 100644 --- a/src/js/HiPS.js +++ b/src/js/HiPS.js @@ -121,6 +121,18 @@ PropertyParser.hipsDataMinmax = function (properties) { return [minData, maxData]; }; +PropertyParser.dataRange = function (properties) { + let range = + properties && + properties.hips_data_range && + properties.hips_data_range.split(" "); + + const minRange = range && parseFloat(range[0]); + const maxRange = range && parseFloat(range[1]); + + return [minRange, maxRange]; +}; + PropertyParser.cutouts = function (properties) { let cuts = properties && @@ -547,6 +559,8 @@ export let HiPS = (function () { self.dataMinMax = PropertyParser.hipsDataMinmax(properties); + self.dataRange = PropertyParser.dataRange(properties); + // HiPS3D special keywords self.orderFreq = (properties && properties.hips_order_freq && +properties.hips_order_freq) || self.orderFreq; self.tileDepth = (properties && properties.hips_tile_depth && +properties.hips_tile_depth) || self.tileDepth; From 87632e3cbe4f4fd49762bedc4926171bb95ed187 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Wed, 27 May 2026 17:16:50 +0200 Subject: [PATCH 11/17] fix #358 --- src/core/src/shaders.rs | 7828 +++++++++++++++++++-------------------- src/js/View.js | 14 +- 2 files changed, 3920 insertions(+), 3922 deletions(-) diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index 1435500b..550affa7 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -3,43 +3,18 @@ use std::collections::HashMap; pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); out.insert( - r"moc_base.frag", + r"image_base.vert", r#"#version 300 es - -precision lowp float; -out vec4 color; - -uniform vec4 u_color; - -void main() { - color = u_color; -}"#, - ); - out.insert( - r"line_base.frag", - r#"#version 300 es - precision highp float; -out vec4 color; -in vec2 l; +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; -uniform vec4 u_color; -uniform float u_thickness; -uniform float u_width; -uniform float u_height; +out vec2 frag_uv; void main() { - if (l.x > 0.05) { - discard; - } else { - color = u_color; - - float dist = abs((u_thickness + 2.0) * l.y); - - float half_thickness = (u_thickness + 2.0) * 0.5; - color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); - } + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; }"#, ); out.insert( @@ -58,820 +33,283 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"line_inst_ndc.vert", + r"line_inst_lonlat.vert", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; layout (location = 2) in vec2 vertex; -out vec2 l; - +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; uniform float u_width; uniform float u_height; uniform float u_thickness; +out vec2 l; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + void main() { - vec2 x_b = p_b - p_a; + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); + + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); float ndc2pix = 2.0 / u_width; - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); }"#, ); out.insert( - r"hips_raytracer_i16.frag", + r"catalogs_aitoff.vert", r#"#version 300 es precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; -uniform sampler2DArray tex; +uniform float current_time; +uniform mat3 inv_model; -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; +out vec2 out_uv; +out vec3 out_p; -uniform Tile textures_tiles[12]; - -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; +const float SQRT_2 = 1.41421356237309504880168872420969808; -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); } -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; } else { - return x02 - 1.0; + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; } + + return vec2(x2d * 0.5, y2d) / SQRT_2; } +const float eps = 1.25e-8; +const int n_iter = 100; -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; } - return 1.0f - p.z; + + return 0.5 * x; } -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; } -int ij2z(int i, int j) { - int i4 = i | (j << 2); +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); - return i5; + return vec3(dc * ts, ds, dc * tc); } -struct HashDxDy { - int idx; - float dx; - float dy; -}; +uniform int u_proj; -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + return w2c_mer(p); } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); } void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); + vec3 p = inv_model * center; - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i16(uv); + vec2 center_pos_clip_space = world2clip_aitoff(p); + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; + out_uv = uv; + out_p = p; }"#, ); out.insert( - r"catalogs_ortho.vert", + r"catalogs_healpix.vert", r#"#version 300 es precision lowp float; layout (location = 0) in vec2 offset; @@ -993,7 +431,7 @@ vec2 proj(vec3 p) { void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_orthographic(p); + vec2 center_pos_clip_space = world2clip_healpix(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); @@ -1003,164 +441,17 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_backcolor.frag", + r"fits_i32.frag", r#"#version 300 es precision lowp float; +precision lowp sampler2D; precision mediump int; out vec4 out_frag_color; +in vec2 frag_uv; -uniform vec3 color; - -void main() { - out_frag_color = vec4(color, 1.0); -}"#, - ); - out.insert( - r"catalogs_mercator.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mercator(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips3d_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; +uniform sampler2D tex; +uniform float opacity; uniform float scale; uniform float offset; @@ -1262,21 +553,6 @@ vec4 apply_tonal(vec4 color) { k_gamma ); } -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} highp float decode_f32(highp vec4 rgba) { highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; @@ -1320,49 +596,6 @@ uint decode_u8(float r) { -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} vec4 val2c_f32(float x) { float alpha = x * scale + offset; @@ -1384,433 +617,32 @@ vec4 val2c(float x) { return apply_tonal(new_color); } -vec4 uvw2c_f32(vec3 uv) { +vec4 uv2c_f32(vec2 uv) { float val = decode_f32(texture(tex, uv).rgba*255.0); return val2c_f32(val); } -vec4 uvw2c_i32(vec3 uv) { +vec4 uv2c_i32(vec2 uv) { float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); + return val2c(val); } -vec4 uvw2c_i16(vec3 uv) { +vec4 uv2c_i16(vec2 uv) { float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); + return val2c(val); } -vec4 uvw2c_u8(vec3 uv) { +vec4 uv2c_u8(vec2 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } -uniform float opacity; - void main() { - vec3 uv = vec3(frag_uv.xyz); + vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - vec4 color = uvw2c_i16(uv); - - out_frag_color = color; + out_frag_color = uv2c_i32(frag_uv); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_f32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_f32(uv0); - vec4 color_end = uvw2c_f32(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; - -out vec3 frag_uv; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; }"#, ); out.insert( @@ -2019,745 +851,16 @@ void main() { }"#, ); out.insert( - r"hips_rasterizer_rgba.frag", + r"hips3d_raster.vert", r#"#version 300 es precision lowp float; -precision lowp sampler2DArray; -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float current_time; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); -}"#, - ); - out.insert( - r"fits_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i16(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"moc_base.vert", - r#"#version 300 es -precision highp float; layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; -uniform mat3 u_2world; +out vec3 frag_uv; + +uniform mat3 inv_model; uniform vec2 ndc_to_clip; uniform float czf; @@ -2864,327 +967,42 @@ vec2 proj(vec3 p) { void main() { vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv = uv; }"#, ); out.insert( - r"hips3d_u8.frag", + r"line_inst_ndc.vert", r#"#version 300 es -precision lowp float; -precision lowp sampler3D; +precision highp float; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; +layout (location = 2) in vec2 vertex; -uniform sampler3D tex; +out vec2 l; -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; + vec2 x_b = p_b - p_a; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - vec4 color = uvw2c_u8(uv); + float ndc2pix = 2.0 / u_width; - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); }"#, ); out.insert( - r"colormaps_colormap.vert", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -layout (location = 0) in vec2 position; -layout (location = 1) in vec2 uv; - -out vec2 out_uv; - -void main() { - gl_Position = vec4(position, 0.f, 1.f); - out_uv = uv; -}"#, - ); - out.insert( - r"colormaps_colormap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -in vec2 out_uv; -out vec4 color; - -uniform sampler2D texture_fbo; -uniform float alpha; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} - -void main() { - float opacity = texture(texture_fbo, out_uv).r; - - float o = smoothstep(0.0, 0.1, opacity); - - color = colormap_f(opacity); - color.a = o * alpha; -}"#, - ); - out.insert( - r"hips_rasterizer_i32.frag", + r"hips_rasterizer_rgba2cmap.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; @@ -3196,6 +1014,7 @@ in vec3 frag_uv_end; in float frag_blending_factor; out vec4 out_frag_color; +uniform float opacity; uniform float scale; uniform float offset; @@ -3439,19 +1258,12 @@ vec4 uvw2c_u8(vec3 uv) { return val2c(val); } -uniform float opacity; - void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i32(uv0); - vec4 color_end = uvw2c_i32(uv1); + vec4 color_start = uvw2cmap_rgba(frag_uv_start); + vec4 color_end = uvw2cmap_rgba(frag_uv_end); out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; + out_frag_color.a = opacity * out_frag_color.a; }"#, ); out.insert( @@ -3852,17 +1664,150 @@ void main() { }"#, ); out.insert( - r"fits_f32.frag", + r"catalogs_ortho.vert", r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_orthographic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips3d_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; uniform float scale; uniform float offset; @@ -3964,6 +1909,21 @@ vec4 apply_tonal(vec4 color) { k_gamma ); } +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} highp float decode_f32(highp vec4 rgba) { highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; @@ -4007,6 +1967,49 @@ uint decode_u8(float r) { +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} vec4 val2c_f32(float x) { float alpha = x * scale + offset; @@ -4028,36 +2031,53 @@ vec4 val2c(float x) { return apply_tonal(new_color); } -vec4 uv2c_f32(vec2 uv) { +vec4 uvw2c_f32(vec3 uv) { float val = decode_f32(texture(tex, uv).rgba*255.0); return val2c_f32(val); } -vec4 uv2c_i32(vec2 uv) { +vec4 uvw2c_i32(vec3 uv) { float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); } -vec4 uv2c_i16(vec2 uv) { +vec4 uvw2c_i16(vec3 uv) { float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); } -vec4 uv2c_u8(vec2 uv) { +vec4 uvw2c_u8(vec3 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } +uniform float opacity; + void main() { - vec2 uv = frag_uv; + vec3 uv = vec3(frag_uv.xyz); uv.y = 1.0 - uv.y; - out_frag_color = uv2c_f32(frag_uv); + vec4 color = uvw2c_i16(uv); + + out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"hips_rasterizer_u8.frag", + r"passes_post_vertex_100es.vert", + r#"#version 300 es +precision mediump float; + +layout (location = 0) in vec2 a_pos; +out vec2 v_tc; + +void main() { + gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); + v_tc = a_pos; +}"#, + ); + out.insert( + r"hips_rasterizer_i32.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; @@ -4320,1134 +2340,11 @@ void main() { uv0.y = 1.0 - uv0.y; uv1.y = 1.0 - uv1.y; - vec4 color_start = uvw2c_u8(uv0); - vec4 color_end = uvw2c_u8(uv1); + vec4 color_start = uvw2c_i32(uv0); + vec4 color_end = uvw2c_i32(uv1); out_frag_color = mix(color_start, color_end, frag_blending_factor); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"image_base.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"hips_raytracer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec2 out_clip_pos; -in vec3 frag_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -uniform float opacity; -uniform vec4 no_tile_color; - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2c_rgba(uv); - - out_frag_color = c; - out_frag_color = vec4(c.rgb, opacity * c.a); -}"#, - ); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"catalogs_healpix.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_healpix(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips3d_f32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_f32(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision mediump float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - -}"#, - ); - out.insert( - r"hips3d_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i32(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -5722,280 +2619,285 @@ void main() { }"#, ); out.insert( - r"catalogs_mollweide.vert", + r"hips3d_u8.frag", r#"#version 300 es precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; +precision lowp sampler3D; -uniform float current_time; -uniform mat3 inv_model; +uniform sampler3D tex; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; +in vec3 frag_uv; -out vec2 out_uv; -out vec3 out_p; +out vec4 out_frag_color; -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); } -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); } -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); + return pow2_f(x, min_value, max_value); } } +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; void main() { - vec3 p = inv_model * center; + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; - vec2 center_pos_clip_space = world2clip_mollweide(p); + vec4 color = uvw2c_u8(uv); - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"line_inst_lonlat.vert", + r"fits_base.vert", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; -layout (location = 2) in vec2 vertex; +precision highp int; -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; -out vec2 l; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} +out vec2 frag_uv; void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); - - vec2 da = p_a_clip - p_b_clip; - - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; }"#, ); out.insert( @@ -6024,7 +2926,139 @@ void main() { }"#, ); out.insert( - r"hips_rasterizer_rgba2cmap.frag", + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); +}"#, + ); + out.insert( + r"hips_rasterizer_rgba.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; @@ -6281,11 +3315,276 @@ vec4 uvw2c_u8(vec3 uv) { } void main() { - vec4 color_start = uvw2cmap_rgba(frag_uv_start); - vec4 color_end = uvw2cmap_rgba(frag_uv_end); + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); out_frag_color = mix(color_start, color_end, frag_blending_factor); out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"catalogs_tan.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_gnomonic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; }"#, ); out.insert( @@ -6308,65 +3607,391 @@ void main() { }"#, ); out.insert( - r"passes_post_vertex_100es.vert", + r"hips3d_i32.frag", r#"#version 300 es -precision mediump float; +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; -layout (location = 0) in vec2 a_pos; -out vec2 v_tc; +uniform sampler3D tex; -void main() { - gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); - v_tc = a_pos; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); -}"#, - ); - out.insert( - r"image_sampler.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; +in vec3 frag_uv; out vec4 out_frag_color; -in vec2 frag_uv; -uniform sampler2D tex; +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + uniform float opacity; void main() { - out_frag_color = texture(tex, frag_uv); + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i32(uv); + + out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"fits_base.vert", + r"moc_base.vert", r#"#version 300 es precision highp float; -precision highp int; +layout (location = 0) in vec2 lonlat; -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; -out vec2 frag_uv; +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.f, 1.f); }"#, ); out.insert( @@ -6766,20 +4391,530 @@ void main() { }"#, ); out.insert( - r"hips3d_red.frag", + r"colormaps_colormap.frag", r#"#version 300 es precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; +precision lowp sampler2D; -uniform sampler3D tex; +in vec2 out_uv; +out vec4 color; -in vec3 frag_uv; +uniform sampler2D texture_fbo; +uniform float alpha; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} + +void main() { + float opacity = texture(texture_fbo, out_uv).r; + + float o = smoothstep(0.0, 0.1, opacity); + + color = colormap_f(opacity); + color.a = o * alpha; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); + out_uv = uv; +}"#, + ); + out.insert( + r"passes_post_fragment_100es.frag", + r#"#version 300 es +precision mediump float; + +in vec2 v_tc; +out vec4 color; + +uniform sampler2D fbo_tex; + +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); +} + +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); +} + +void main() { + color = texture(fbo_tex, v_tc); + +}"#, + ); + out.insert( + r"hips_raytracer_rgba2cmap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp sampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2cmap_rgba(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"image_sampler.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; uniform float opacity; +void main() { + out_frag_color = texture(tex, frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"moc_base.frag", + r#"#version 300 es + +precision lowp float; +out vec4 color; + +uniform vec4 u_color; + +void main() { + color = u_color; +}"#, + ); + out.insert( + r"hips_rasterizer_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + uniform float scale; uniform float offset; uniform float blank; @@ -7022,12 +5157,1012 @@ vec4 uvw2c_u8(vec3 uv) { return val2c(val); } -void main() { - vec3 uv = vec3(frag_uv.xyz); - vec4 color = uvw2c_ra(uv); +uniform float opacity; - out_frag_color = color; - out_frag_color.a = opacity * out_frag_color.a; +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_u8(uv0); + vec4 color_end = uvw2c_u8(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_rgba.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec2 out_clip_pos; +in vec3 frag_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +uniform float opacity; +uniform vec4 no_tile_color; + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2c_rgba(uv); + + out_frag_color = c; + out_frag_color = vec4(c.rgb, opacity * c.a); +}"#, + ); + out.insert( + r"fits_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i16(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_i16.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i16(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -7431,11 +6566,282 @@ void main() { }"#, ); out.insert( - r"fits_i32.frag", + r"hips_rasterizer_f32.frag", r#"#version 300 es precision lowp float; -precision lowp sampler2D; -precision mediump int; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_f32(uv0); + vec4 color_end = uvw2c_f32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; in vec2 frag_uv; @@ -7631,15 +7037,78 @@ void main() { vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - out_frag_color = uv2c_i32(frag_uv); + out_frag_color = uv2c_f32(frag_uv); out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"catalogs_tan.vert", + r"hips_raytracer_backcolor.frag", + r#"#version 300 es +precision lowp float; +precision mediump int; + +out vec4 out_frag_color; + +uniform vec3 color; + +void main() { + out_frag_color = vec4(color, 1.0); +}"#, + ); + out.insert( + r"catalogs_ortho.frag", r#"#version 300 es precision lowp float; +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"line_base.frag", + r#"#version 300 es + +precision highp float; + +out vec4 color; +in vec2 l; + +uniform vec4 u_color; +uniform float u_thickness; +uniform float u_width; +uniform float u_height; + +void main() { + if (l.x > 0.05) { + discard; + } else { + color = u_color; + + float dist = abs((u_thickness + 2.0) * l.y); + + float half_thickness = (u_thickness + 2.0) * 0.5; + color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); + } +}"#, + ); + out.insert( + r"catalogs_arc.vert", + r#"#version 300 es +precision lowp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; @@ -7755,11 +7224,10 @@ vec2 proj(vec3 p) { } } - void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_gnomonic(p); + vec2 center_pos_clip_space = world2clip_arc(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); @@ -7769,7 +7237,7 @@ void main() { }"#, ); out.insert( - r"catalogs_aitoff.vert", + r"catalogs_mercator.vert", r#"#version 300 es precision lowp float; layout (location = 0) in vec2 offset; @@ -7887,16 +7355,548 @@ vec2 proj(vec3 p) { } } + void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_aitoff(p); + vec2 center_pos_clip_space = world2clip_mercator(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); out_uv = uv; out_p = p; +}"#, + ); + out.insert( + r"hips3d_red.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec3 uv = vec3(frag_uv.xyz); + vec4 color = uvw2c_ra(uv); + + out_frag_color = color; + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips3d_f32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_f32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out diff --git a/src/js/View.js b/src/js/View.js index 83c7e664..48780e8e 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -2111,10 +2111,8 @@ export let View = (function () { View.prototype.addImageLayer = function (imageLayer, layer) { let self = this; // start the query - const imageLayerPromise = imageLayer.query; - - let idx = this.promises.length; - this.promises.push(imageLayerPromise); + const promise = imageLayer.query; + this.promises.push(promise); // All image layer promises must be completed (fullfilled or rejected) const task = { @@ -2125,7 +2123,7 @@ export let View = (function () { ALEvent.FETCH.dispatchedTo(document, {task}); // All the remaining promises must be terminated and the current one must be resolved // so that we can add it to the view (call of _add2View) - Promise.all([Promise.allSettled(this.promises), imageLayerPromise]) + Promise.all([Promise.allSettled(this.promises), promise]) // Then we add the layer to the view .then((_) => imageLayer._addToView(layer)) // Then we keep a track of the layer in the JS front @@ -2159,11 +2157,11 @@ export let View = (function () { ALEvent.RESOURCE_FETCHED.dispatchedTo(document, {task}); self.imageLayersBeingQueried.delete(layer); - // Remove the settled promise - this.promises.splice(idx, 1); + this.promises = this.promises.filter(p => p !== promise); const waitsForLayer = this._waitsForLayer(); + if (!waitsForLayer && this.delayedBaseLayerCalledParams && this.delayedBaseLayerCalledParams !== layer) { this.aladin.setBaseImageLayer(this.delayedBaseLayerCalledParams) this.delayedBaseLayerCalledParams = null; @@ -2171,7 +2169,7 @@ export let View = (function () { return; } - if (!waitsForLayer && self.empty) { + if (!waitsForLayer && this.empty) { // no promises to launch and the view has no HiPS. // This situation can occurs if the MOCServer is out // If so we can directly put the url of the DSS hosted in alasky, From a247704f417b0c8ed6563424ca2834bf15f02943 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Thu, 28 May 2026 15:00:47 +0200 Subject: [PATCH 12/17] should fix #330, refac keyword heuristic from the HiPS standard --- examples/al-aas225.html | 2 +- examples/al-badbaselayer.html | 6 +- examples/al-image-with-AVM-tags.html | 1 - examples/al-rubin.html | 36 + examples/al-zoomchanged.html | 2 +- src/core/al-api/src/coo_system.rs | 4 +- src/core/src/shaders.rs | 10324 ++++++++++++------------- src/js/Aladin.js | 19 +- src/js/CooFrameEnum.js | 16 + src/js/HiPS.js | 67 +- src/js/Image.js | 1 - src/js/Utils.ts | 1 - 12 files changed, 5258 insertions(+), 5221 deletions(-) create mode 100644 examples/al-rubin.html diff --git a/examples/al-aas225.html b/examples/al-aas225.html index c7221cd4..6dc14d3c 100644 --- a/examples/al-aas225.html +++ b/examples/al-aas225.html @@ -9,7 +9,7 @@ let aladin; A.init.then(() => { aladin = A.aladin('#aladin-lite-div', { fov:0.15, target: 'Arp 240', showContextMenu: true, showReticle: false, fullScreen: true }); - aladin.setBaseImageLayer(aladin.newImageSurvey('P/SDSS9/g', {colormap: "eosb", stretch: "linear"})); + aladin.setBaseImageLayer(aladin.newImageSurvey('P/SDSS9/g', {url: 'https://alasky.cds.unistra.fr/SDSS/DR9/band-g/', maxOrder: 10, cooFrame: 'icrs', imgFormat: 'png', colormap: "eosb", stretch: "linear"})); var simbad = A.catalog({name: 'Simbad', sourceSize: 16, color: '#4050F0'}); aladin.addCatalog(simbad); diff --git a/examples/al-badbaselayer.html b/examples/al-badbaselayer.html index 21143eb5..72aecdd4 100644 --- a/examples/al-badbaselayer.html +++ b/examples/al-badbaselayer.html @@ -7,10 +7,10 @@ diff --git a/examples/al-image-with-AVM-tags.html b/examples/al-image-with-AVM-tags.html index 559d892e..29c8070c 100644 --- a/examples/al-image-with-AVM-tags.html +++ b/examples/al-image-with-AVM-tags.html @@ -14,7 +14,6 @@ { name: "spitzer-ssc2005-24a1", successCallback: (ra, dec, fov, image) => { - console.log(ra, dec) aladin.gotoRaDec(ra, dec); aladin.setFoV(fov * 5) } diff --git a/examples/al-rubin.html b/examples/al-rubin.html new file mode 100644 index 00000000..ec6b9eaf --- /dev/null +++ b/examples/al-rubin.html @@ -0,0 +1,36 @@ + + + + + +
    + + + \ No newline at end of file diff --git a/examples/al-zoomchanged.html b/examples/al-zoomchanged.html index decd2414..c3acd6cb 100644 --- a/examples/al-zoomchanged.html +++ b/examples/al-zoomchanged.html @@ -21,7 +21,7 @@ console.log("Rotation just changed to ", rotation); }) aladin.on("positionChanged", ({ra, dec}) => { - console.log('call to aladin', aladin.pix2world(300, 300, "galactic")) + console.log('call to aladin', aladin.pix2world(300, 300, 'gal')) console.log('positionChanged in icrs', ra, dec) }) diff --git a/src/core/al-api/src/coo_system.rs b/src/core/al-api/src/coo_system.rs index b3f267aa..eb623980 100644 --- a/src/core/al-api/src/coo_system.rs +++ b/src/core/al-api/src/coo_system.rs @@ -31,8 +31,8 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] #[derive(Clone, Copy, PartialEq, Eq, Debug, Deserialize, Hash)] pub enum CooSystem { - ICRS = 0, - GAL = 1, + ICRS, + GAL, } pub const NUM_COOSYSTEM: usize = 2; diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index 550affa7..7f3bc81c 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -3,445 +3,34 @@ use std::collections::HashMap; pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); out.insert( - r"image_base.vert", + r"line_base.frag", r#"#version 300 es + precision highp float; -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; +out vec4 color; +in vec2 l; -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; - -uniform vec2 ndc_to_clip; -uniform float czf; - -void main() { - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); -}"#, - ); - out.insert( - r"line_inst_lonlat.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; -layout (location = 2) in vec2 vertex; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; +uniform vec4 u_color; +uniform float u_thickness; uniform float u_width; uniform float u_height; -uniform float u_thickness; - -out vec2 l; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); + if (l.x > 0.05) { + discard; + } else { + color = u_color; - vec2 da = p_a_clip - p_b_clip; + float dist = abs((u_thickness + 2.0) * l.y); - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); + float half_thickness = (u_thickness + 2.0) * 0.5; + color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); + } }"#, ); out.insert( - r"catalogs_aitoff.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_aitoff(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_healpix.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_healpix(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"fits_i32.frag", + r"fits_i16.frag", r#"#version 300 es precision lowp float; precision lowp sampler2D; @@ -641,364 +230,8 @@ void main() { vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - out_frag_color = uv2c_i32(frag_uv); + out_frag_color = uv2c_i16(frag_uv); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_u8(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; - -out vec3 frag_uv; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; -}"#, - ); - out.insert( - r"line_inst_ndc.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; -layout (location = 2) in vec2 vertex; - -out vec2 l; - -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -void main() { - vec2 x_b = p_b - p_a; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); }"#, ); out.insert( @@ -1267,404 +500,7 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_u8(uv); - - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_ortho.vert", + r"catalogs_healpix.vert", r#"#version 300 es precision lowp float; layout (location = 0) in vec2 offset; @@ -1786,7 +622,7 @@ vec2 proj(vec3 p) { void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_orthographic(p); + vec2 center_pos_clip_space = world2clip_healpix(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); @@ -2064,2811 +900,18 @@ void main() { }"#, ); out.insert( - r"passes_post_vertex_100es.vert", - r#"#version 300 es -precision mediump float; - -layout (location = 0) in vec2 a_pos; -out vec2 v_tc; - -void main() { - gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); - v_tc = a_pos; -}"#, - ); - out.insert( - r"hips_rasterizer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i32(uv0); - vec4 color_end = uvw2c_i32(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i16(uv0); - vec4 color_end = uvw2c_i16(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_u8(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_base.vert", - r#"#version 300 es -precision highp float; -precision highp int; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"hips_raytracer_raytracer.vert", + r"hips_raytracer_backcolor.vert", r#"#version 300 es precision lowp float; precision mediump int; layout (location = 0) in vec2 pos_clip_space; -layout (location = 1) in vec3 pos_world_space; - -out vec2 out_clip_pos; -out vec3 frag_pos; uniform vec2 ndc_to_clip; uniform float czf; -uniform mat3 model; void main() { - vec2 uv = pos_clip_space * 0.5 + 0.5; - - frag_pos = model * pos_world_space; - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); - out_clip_pos = pos_clip_space; -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float current_time; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); -}"#, - ); - out.insert( - r"hips_rasterizer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"catalogs_tan.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_gnomonic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_mollweide.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mollweide(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_catalog.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"hips3d_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i32(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"moc_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 lonlat; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); -}"#, - ); - out.insert( - r"hips_raytracer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; - -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i32(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"colormaps_colormap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -in vec2 out_uv; -out vec4 color; - -uniform sampler2D texture_fbo; -uniform float alpha; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} - -void main() { - float opacity = texture(texture_fbo, out_uv).r; - - float o = smoothstep(0.0, 0.1, opacity); - - color = colormap_f(opacity); - color.a = o * alpha; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); -}"#, - ); - out.insert( - r"colormaps_colormap.vert", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; - -layout (location = 0) in vec2 position; -layout (location = 1) in vec2 uv; - -out vec2 out_uv; - -void main() { - gl_Position = vec4(position, 0.f, 1.f); - out_uv = uv; -}"#, - ); - out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision mediump float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - -}"#, - ); - out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -4889,885 +932,148 @@ void main() { }"#, ); out.insert( - r"moc_base.frag", + r"line_inst_lonlat.vert", r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; +layout (location = 2) in vec2 vertex; -precision lowp float; -out vec4 color; +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; -uniform vec4 u_color; +out vec2 l; -void main() { - color = u_color; -}"#, - ); - out.insert( - r"hips_rasterizer_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_u8(uv0); - vec4 color_end = uvw2c_u8(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec2 out_clip_pos; -in vec3 frag_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; +const float SQRT_2 = 1.41421356237309504880168872420969808; -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); } -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; } else { - return x02 - 1.0; + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; } + + return vec2(x2d * 0.5, y2d) / SQRT_2; } +const float eps = 1.25e-8; +const int n_iter = 100; -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; } - return 1.0f - p.z; + + return 0.5 * x; } -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; } -int ij2z(int i, int j) { - int i4 = i | (j << 2); +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); - return i5; + return vec3(dc * ts, ds, dc * tc); } -struct HashDxDy { - int idx; - float dx; - float dy; -}; +uniform int u_proj; -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + return w2c_mer(p); } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -uniform float opacity; -uniform vec4 no_tile_color; - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2c_rgba(uv); - - out_frag_color = c; - out_frag_color = vec4(c.rgb, opacity * c.a); -}"#, - ); - out.insert( - r"fits_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); } void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); - out_frag_color = uv2c_i16(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); }"#, ); out.insert( @@ -6163,6 +1469,584 @@ void main() { out_frag_color = c; out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_ortho.frag", + r#"#version 300 es +precision lowp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"hips_rasterizer_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i32(uv0); + vec4 color_end = uvw2c_i32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"passes_post_vertex_100es.vert", + r#"#version 300 es +precision mediump float; + +layout (location = 0) in vec2 a_pos; +out vec2 v_tc; + +void main() { + gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); + v_tc = a_pos; +}"#, + ); + out.insert( + r"hips_rasterizer_f32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_f32(uv0); + vec4 color_end = uvw2c_f32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -6566,7 +2450,7 @@ void main() { }"#, ); out.insert( - r"hips_rasterizer_f32.frag", + r"hips_rasterizer_i16.frag", r#"#version 300 es precision lowp float; precision lowp sampler2DArray; @@ -6829,19 +2713,19 @@ void main() { uv0.y = 1.0 - uv0.y; uv1.y = 1.0 - uv1.y; - vec4 color_start = uvw2c_f32(uv0); - vec4 color_end = uvw2c_f32(uv1); + vec4 color_start = uvw2c_i16(uv0); + vec4 color_end = uvw2c_i16(uv1); out_frag_color = mix(color_start, color_end, frag_blending_factor); out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"fits_f32.frag", + r"fits_u8.frag", r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; +precision lowp float; +precision lowp sampler2D; +precision mediump int; out vec4 out_frag_color; in vec2 frag_uv; @@ -7037,335 +2921,8 @@ void main() { vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - out_frag_color = uv2c_f32(frag_uv); + out_frag_color = uv2c_u8(frag_uv); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.frag", - r#"#version 300 es -precision lowp float; -precision mediump int; - -out vec4 out_frag_color; - -uniform vec3 color; - -void main() { - out_frag_color = vec4(color, 1.0); -}"#, - ); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"line_base.frag", - r#"#version 300 es - -precision highp float; - -out vec4 color; -in vec2 l; - -uniform vec4 u_color; -uniform float u_thickness; -uniform float u_width; -uniform float u_height; - -void main() { - if (l.x > 0.05) { - discard; - } else { - color = u_color; - - float dist = abs((u_thickness + 2.0) * l.y); - - float half_thickness = (u_thickness + 2.0) * 0.5; - color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); - } -}"#, - ); - out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_mercator.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mercator(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; }"#, ); out.insert( @@ -7631,6 +3188,1085 @@ void main() { out_frag_color = color; out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips3d_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_u8(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"passes_post_fragment_100es.frag", + r#"#version 300 es +precision mediump float; + +in vec2 v_tc; +out vec4 color; + +uniform sampler2D fbo_tex; + +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); +} + +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); +} + +void main() { + color = texture(fbo_tex, v_tc); + +}"#, + ); + out.insert( + r"fits_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; +precision mediump int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_f32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_raytracer.vert", + r#"#version 300 es +precision lowp float; +precision mediump int; + +layout (location = 0) in vec2 pos_clip_space; +layout (location = 1) in vec3 pos_world_space; + +out vec2 out_clip_pos; +out vec3 frag_pos; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform mat3 model; + +void main() { + vec2 uv = pos_clip_space * 0.5 + 0.5; + + frag_pos = model * pos_world_space; + + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); + out_clip_pos = pos_clip_space; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.frag", + r#"#version 300 es +precision lowp float; +precision mediump int; + +out vec4 out_frag_color; + +uniform vec3 color; + +void main() { + out_frag_color = vec4(color, 1.0); +}"#, + ); + out.insert( + r"colormaps_colormap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +in vec2 out_uv; +out vec4 color; + +uniform sampler2D texture_fbo; +uniform float alpha; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} + +void main() { + float opacity = texture(texture_fbo, out_uv).r; + + float o = smoothstep(0.0, 0.1, opacity); + + color = colormap_f(opacity); + color.a = o * alpha; +}"#, + ); + out.insert( + r"catalogs_aitoff.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_aitoff(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"moc_base.frag", + r#"#version 300 es + +precision lowp float; +out vec4 color; + +uniform vec4 u_color; + +void main() { + color = u_color; +}"#, + ); + out.insert( + r"catalogs_mercator.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mercator(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision lowp float; +precision lowp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); + out_uv = uv; }"#, ); out.insert( @@ -7897,6 +4533,3370 @@ void main() { out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_ortho.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_orthographic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_raytracer_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp usampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_u8(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"image_base.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips_raytracer_rgba2cmap.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp sampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2cmap_rgba(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_catalog.frag", + r#"#version 300 es +precision lowp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"fits_base.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"hips3d_raster.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; + +out vec3 frag_uv; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv = uv; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"moc_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 lonlat; + +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.f, 1.f); +}"#, + ); + out.insert( + r"hips_rasterizer_u8.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_u8(uv0); + vec4 color_end = uvw2c_u8(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_arc.vert", + r#"#version 300 es +precision lowp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_arc(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"line_inst_ndc.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; +layout (location = 2) in vec2 vertex; + +out vec2 l; + +uniform float u_width; +uniform float u_height; +uniform float u_thickness; + +void main() { + vec2 x_b = p_b - p_a; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); +}"#, + ); + out.insert( + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); +}"#, + ); + out.insert( + r"catalogs_tan.vert", + r#"#version 300 es +precision lowp float; + +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_gnomonic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_raytracer_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision lowp usampler2DArray; +precision lowp isampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i32(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_i32.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler3D; +precision lowp isampler3D; +precision lowp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_rgba.frag", + r#"#version 300 es +precision lowp float; +precision lowp sampler2DArray; +precision mediump int; + +uniform sampler2DArray tex; + +in vec2 out_clip_pos; +in vec3 frag_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +uniform float opacity; +uniform vec4 no_tile_color; + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2c_rgba(uv); + + out_frag_color = c; + out_frag_color = vec4(c.rgb, opacity * c.a); }"#, ); out diff --git a/src/js/Aladin.js b/src/js/Aladin.js index c9d88500..a449e73d 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -1958,7 +1958,7 @@ export let Aladin = (function () { return this.createImageSurvey( id, options && options.name, - id, + options && options.url || id, options && options.cooFrame, options && options.maxOrder, options @@ -2211,7 +2211,7 @@ export let Aladin = (function () { * view in the counter clockwise order (or towards the east) */ Aladin.prototype.setRotation = function (rotation) { - if (!rotation) { + if (Utils.isNumber(rotation)) { console.warn("Rotation angle is not valid:", rotation) return; } @@ -2808,13 +2808,8 @@ export let Aladin = (function () { if (typeof frame === "string") { frame = CooFrameEnum.fromString(frame, CooFrameEnum.ICRS); } - - // Map to the numeric wasm-bindgen CooSystem discriminant - frame = (frame.system === CooFrameEnum.SYSTEMS.GAL) - ? Aladin.wasmLibs.core.CooSystem.GAL - : Aladin.wasmLibs.core.CooSystem.ICRS; } - let [lon, lat] = this.view.wasm.pix2world(x, y, frame); + let [lon, lat] = this.view.wasm.pix2world(x, y, CooFrameEnum.toWasm(frame)); return [lon < 0 ? lon + 360.0 : lon, lat]; }; @@ -2836,16 +2831,10 @@ export let Aladin = (function () { if (typeof frame === "string") { frame = CooFrameEnum.fromString(frame, CooFrameEnum.ICRS); } - - // Map to the numeric wasm-bindgen CooSystem discriminant - frame = (frame.system === CooFrameEnum.SYSTEMS.GAL) - ? Aladin.wasmLibs.core.CooSystem.GAL - : Aladin.wasmLibs.core.CooSystem.ICRS; } // frame === undefined/null → passed as-is, WASM treats as None (default system) - - return this.view.wasm.world2pix(lon, lat, frame); + return this.view.wasm.world2pix(lon, lat, CooFrameEnum.toWasm(frame)); }; /** diff --git a/src/js/CooFrameEnum.js b/src/js/CooFrameEnum.js index 470a527d..30cdfa80 100644 --- a/src/js/CooFrameEnum.js +++ b/src/js/CooFrameEnum.js @@ -29,6 +29,8 @@ * *****************************************************************************/ +import { Aladin } from "./Aladin"; + export let CooFrameEnum = (function() { // Corresponds to the Rust CooSystem enum possibilities. @@ -59,6 +61,20 @@ export let CooFrameEnum = (function() { else { return defaultValue ? defaultValue : null; } + }, + + toWasm: function(cooFrame) { + let frame = null; + + let system = cooFrame?.system; + + if (system === CooFrameEnum.SYSTEMS.ICRS) { + frame = Aladin.wasmLibs.core.CooSystem.ICRS; + } else if (system === CooFrameEnum.SYSTEMS.GAL) { + frame = Aladin.wasmLibs.core.CooSystem.GAL; + } + + return frame; } }; diff --git a/src/js/HiPS.js b/src/js/HiPS.js index 5f1b9f7d..0941841a 100644 --- a/src/js/HiPS.js +++ b/src/js/HiPS.js @@ -42,11 +42,10 @@ PropertyParser.tileSize = function (properties) { let tileSize = (properties && properties.hips_tile_width && - +properties.hips_tile_width) || - 512; + +properties.hips_tile_width) // Check if the tile width size is a power of 2 - if (tileSize & (tileSize - 1 !== 0)) { + if (tileSize && ((tileSize & (tileSize - 1)) !== 0)) { tileSize = 512; } @@ -175,11 +174,11 @@ PropertyParser.isPlanetaryBody = function (properties) { * @property {Function} [successCallback] - A callback executed when the HiPS has been loaded * @property {Function} [errorCallback] - A callback executed when the HiPS could not be loaded * @property {string} [imgFormat] - Formats accepted 'webp', 'png', 'jpeg' or 'fits'. Will raise an error if the HiPS does not contain tiles in this format - * @property {CooFrame} [cooFrame] - Coordinate frame of the survey tiles. If not given, the one from the parsed properties file will be retrieved. + * @property {string} [cooFrame] - Coordinate frame of the survey tiles. If not given, the one from the parsed properties file will be retrieved. Possible values: 'ICRS', 'ICRSd', 'galactic', 'equatorial', 'j2000' * @property {number} [maxOrder] - The maximum HEALPix order of the HiPS, i.e the HEALPix order of the most refined tile images of the HiPS. - * @property {number} [numBitsPerPixel] - Useful if you want to display the FITS tiles of a HiPS. It specifies the number of bits per pixel. Possible values are: + * @property {number} [bitpix] - Useful if you want to display the FITS tiles of a HiPS. It specifies the number of bits per pixel. Possible values are: * -64: double, -32: float, 8: unsigned byte, 16: short, 32: integer 32 bits, 64: integer 64 bits - * @property {number} [tileSize] - The width of the HEALPix tile images. Mostly 512 pixels but can be 256, 128, 64, 32 + * @property {number} [tileSize=512] - The width of the HEALPix tile images. Mostly 512 pixels but can be 256, 128, 64, 32 * @property {number} [minOrder] - If not given, retrieved from the properties of the survey. * @property {boolean} [longitudeReversed] - Deprecated The longitudeReversed property is now deprecated since version 3.6.1. This property has been removed since version 3.7.0 and replaced with {@link Aladin#reverseLongitude} set directly on the {@link Aladin} view object and not at the HiPS level. * @property {number} [opacity=1.0] - Opacity of the survey or image (value between 0 and 1). @@ -281,7 +280,7 @@ export let HiPS = (function () { */ function HiPS(id, location, options) { this.added = false; - // Unique identifier for a survey + // Required ID, synonym of the CreatorDid of a HiPS this.id = id; this.name = (options && options.name) || id; @@ -329,20 +328,23 @@ export let HiPS = (function () { this.url = location; + // Max order Required this.maxOrder = options && options.maxOrder; this.minOrder = (options && options.minOrder) || 0; + // Frame Required this.cooFrame = options && CooFrameEnum.fromString(options.cooFrame, null)?.system; - this.tileSize = options && options.tileSize; + this.tileSize = options && options.tileSize || 512; this.skyFraction = options && options.skyFraction; + // Image format Required this.imgFormat = options && options.imgFormat; - this.formats = options && options.formats; + this.formats = (options && options.formats) || (this.imgFormat && [this.imgFormat]); this.defaultFitsMinCut = options && options.defaultFitsMinCut; this.defaultFitsMaxCut = options && options.defaultFitsMaxCut; - this.numBitsPerPixel = options && options.numBitsPerPixel; - this.creatorDid = options && options.creatorDid; + this.bitpix = options && options.bitpix; + this.creatorDid = (options && options.creatorDid) || this.id || this.url; this.errorCallback = options && options.errorCallback; this.successCallback = options && options.successCallback; - this.dataproductType = options && options.dataproductType; + this.dataproductType = options && options.dataproductType || 'image'; this.tileDepth = options && options.tileDepth; this.orderFreq = options && options.orderFreq; this.emMin = options && options.emMin; @@ -410,34 +412,33 @@ export let HiPS = (function () { resolve(self); }); } else { - let isIncompleteOptions = true; + let mustRequestProperties = true; - let isID = Utils.isUrl(this.url) === undefined; + let isUrl = Utils.isUrl(this.url) !== undefined; if (this.imgFormat === "fits") { // a fits is given - isIncompleteOptions = !( + mustRequestProperties = !( this.maxOrder && - (!isID && this.url) && + isUrl && this.imgFormat && - this.tileSize && this.cooFrame && - this.numBitsPerPixel + // TODO: this should not be mandatory, one just parse FITS files + this.bitpix ); } else { - isIncompleteOptions = !( + mustRequestProperties = !( this.maxOrder && - (!isID && this.url) && + isUrl && this.imgFormat && - this.tileSize && this.cooFrame ); } this.query = new Promise(async (resolve, reject) => { - if (isIncompleteOptions) { + if (mustRequestProperties) { // ID typed url - if (self.startUrl && isID) { + if (self.startUrl && !isUrl) { // First download the properties from the start url await HiPSProperties.fetchFromUrl(self.startUrl, self.requestMode, self.requestCredentials) .then((p) => { @@ -465,7 +466,7 @@ export let HiPS = (function () { }, 1000 ); - } else if (!self.startUrl && isID) { + } else if (!self.startUrl && !isUrl) { // the url stores a "CDS ID" we take it prioritaly // if the url is null, take the id, this is for some tests // to pass because some users might just give null as url param and a "CDS ID" as id param @@ -495,12 +496,12 @@ export let HiPS = (function () { .catch((e) => reject("HiPS " + self.id + " error: HiPS not found at url " + self.url + "\nReason: " + e.stack)) } } else { - self._parseProperties({ + /*self._parseProperties({ hips_order: self.maxOrder, hips_service_url: self.url, hips_tile_width: self.tileSize, hips_frame: self.cooFrame - }) + })*/ } if (self.updateHiPSCache) { @@ -581,7 +582,7 @@ export let HiPS = (function () { // Tile formats self.formats = - PropertyParser.formats(properties) || self.formats || ["jpeg"]; + PropertyParser.formats(properties) || self.formats; // Min order const minOrder = PropertyParser.minOrder(properties) @@ -594,7 +595,7 @@ export let HiPS = (function () { PropertyParser.cooFrame(properties); // Parse the cooframe from the properties but if it fails, take the one given by the user // If the user gave nothing, then take ICRS as the default one - self.cooFrame = CooFrameEnum.fromString(cooFrame, self.cooFrame || CooFrameEnum.ICRS).system; + self.cooFrame = CooFrameEnum.fromString(cooFrame, null)?.system || self.cooFrame || CooFrameEnum.ICRS.system; // sky fraction self.skyFraction = PropertyParser.skyFraction(properties); @@ -616,8 +617,8 @@ export let HiPS = (function () { self.defaultFitsMaxCut = cutoutFromProperties[1] || 1.0; // Bitpix - self.numBitsPerPixel = - PropertyParser.bitpix(properties) || self.numBitsPerPixel; + self.bitpix = + PropertyParser.bitpix(properties) || self.bitpix; // HiPS body if (properties.hips_body) { @@ -632,8 +633,6 @@ export let HiPS = (function () { self.name = self.name || self.id || self.url; self.name = self.name.replace(/ +/g, ' '); - self.creatorDid = self.creatorDid || self.id || self.url; - // check the imgFormat with respect to the formats accepted image format const chooseTileFormat = (formats) => { if (formats.indexOf("webp") >= 0) { @@ -1254,7 +1253,7 @@ export let HiPS = (function () { cooFrame: self.cooFrame, tileSize: self.tileSize, formats: self.formats, - numBitsPerPixel: self.numBitsPerPixel, + bitpix: self.bitpix, skyFraction: self.skyFraction, minOrder: self.minOrder, initialFov: self.initialFov, @@ -1325,7 +1324,7 @@ export let HiPS = (function () { cooFrame: self.cooFrame, tileSize: self.tileSize, formats: self.formats, - bitpix: self.numBitsPerPixel, + bitpix: self.bitpix, skyFraction: self.skyFraction, minOrder: self.minOrder, initialFov: self.initialFov, diff --git a/src/js/Image.js b/src/js/Image.js index a57c7a38..ba5033f7 100644 --- a/src/js/Image.js +++ b/src/js/Image.js @@ -559,7 +559,6 @@ export let Image = (function () { let self = this; let img = document.createElement('img'); - console.log(this.url) let checkImgFormat = Utils.fetchWithProxy({url: this.url, dataType: 'arrayBuffer'}) .then(arrayBuffer => { const view = new DataView(arrayBuffer); diff --git a/src/js/Utils.ts b/src/js/Utils.ts index 71669471..644d476c 100644 --- a/src/js/Utils.ts +++ b/src/js/Utils.ts @@ -439,7 +439,6 @@ Utils.fetchWithProxy = function(params) { console.info("Trying querying the proxy:" + JSONP_PROXY) // Try as CORS const url = JSONP_PROXY + '?url=' + params.url; - console.log(url) return Utils.fetch({ ...params, url, From 3d10dce89e5d85b9fa8b1fbd4f4539f4978897d9 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Thu, 28 May 2026 16:20:46 +0200 Subject: [PATCH 13/17] attempt to fix #295 on old safari mac machines --- .zenodo.json | 2 +- LICENSE | 457 ++++++------------ README.md | 2 +- codemeta.json | 2 +- package.json | 2 +- src/glsl/webgl2/catalogs/aitoff.vert | 2 +- src/glsl/webgl2/catalogs/arc.vert | 2 +- src/glsl/webgl2/catalogs/catalog.frag | 2 +- src/glsl/webgl2/catalogs/healpix.vert | 3 +- src/glsl/webgl2/catalogs/mercator.vert | 2 +- src/glsl/webgl2/catalogs/mollweide.vert | 2 +- src/glsl/webgl2/catalogs/ortho.frag | 2 +- src/glsl/webgl2/catalogs/ortho.vert | 2 +- src/glsl/webgl2/catalogs/tan.vert | 2 +- src/glsl/webgl2/colormaps/colormap.frag | 4 +- src/glsl/webgl2/colormaps/colormap.vert | 4 +- src/glsl/webgl2/fits/i16.frag | 6 +- src/glsl/webgl2/fits/i32.frag | 6 +- src/glsl/webgl2/fits/u8.frag | 6 +- src/glsl/webgl2/hips/rasterizer/f32.frag | 4 +- src/glsl/webgl2/hips/rasterizer/i16.frag | 4 +- src/glsl/webgl2/hips/rasterizer/i32.frag | 4 +- src/glsl/webgl2/hips/rasterizer/rgba.frag | 4 +- .../webgl2/hips/rasterizer/rgba2cmap.frag | 4 +- src/glsl/webgl2/hips/rasterizer/u8.frag | 4 +- src/glsl/webgl2/hips/raytracer/backcolor.frag | 4 +- src/glsl/webgl2/hips/raytracer/backcolor.vert | 4 +- src/glsl/webgl2/hips/raytracer/f32.frag | 6 +- src/glsl/webgl2/hips/raytracer/i16.frag | 6 +- src/glsl/webgl2/hips/raytracer/i32.frag | 10 +- src/glsl/webgl2/hips/raytracer/raytracer.vert | 4 +- src/glsl/webgl2/hips/raytracer/rgba.frag | 6 +- src/glsl/webgl2/hips/raytracer/rgba2cmap.frag | 10 +- src/glsl/webgl2/hips/raytracer/u8.frag | 10 +- src/glsl/webgl2/hips3d/f32.frag | 4 +- src/glsl/webgl2/hips3d/i16.frag | 8 +- src/glsl/webgl2/hips3d/i32.frag | 8 +- src/glsl/webgl2/hips3d/raster.vert | 2 +- src/glsl/webgl2/hips3d/red.frag | 8 +- src/glsl/webgl2/hips3d/u8.frag | 4 +- src/glsl/webgl2/moc/base.frag | 2 +- .../webgl2/passes/post_fragment_100es.frag | 2 +- src/glsl/webgl2/passes/post_vertex_100es.vert | 2 +- 43 files changed, 248 insertions(+), 386 deletions(-) diff --git a/.zenodo.json b/.zenodo.json index 1b3f3d86..1c5d70e1 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,5 +1,5 @@ { - "license": "GPL-3.0", + "license": "LGPL-3.0-or-later", "access_right": "open", "description": "An astronomical HiPS visualizer in the browser ", "keywords": ["astronomy", "visualizer", "images"], diff --git a/LICENSE b/LICENSE index 51ce144d..153d416d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,304 +1,165 @@ -GNU LESSER GENERAL PUBLIC LICENSE -Version 3, 29 June 2007 + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 -Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. -Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. -This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. -0. Additional Definitions. + 0. Additional Definitions. -As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. -"The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. - -An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. - -A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". - -The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. - -The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. - -1. Exception to Section 3 of the GNU GPL. -You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. - -2. Conveying Modified Versions. -If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: - - a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. - -3. Object Code Incorporating Material from Library Header Files. -The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license document. - -4. Combined Works. -You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: - - a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license document. - - c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. - - e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) - -5. Combined Libraries. -You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. - -6. Revised Versions of the GNU Lesser General Public License. -The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. - -If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. - -GNU GENERAL PUBLIC LICENSE -Version 3, 29 June 2007 - -Copyright © 2007 Free Software Foundation, Inc. - -Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - -Preamble - -The GNU General Public License is a free, copyleft license for software and other kinds of works. - -The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. - -When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - -To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. - -For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. - -Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. - -For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. - -Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. - -Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. - -The precise terms and conditions for copying, distribution and modification follow. - -TERMS AND CONDITIONS - -0. Definitions. - -“This License” refers to version 3 of the GNU General Public License. - -“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. - -“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. - -To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work. - -A “covered work” means either the unmodified Program or a work based on the Program. - -To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. - -To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. - -An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. - -1. Source Code. -The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work. - -A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. - -The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. - -The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. - -The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. - -The Corresponding Source for a work in source code form is that same work. - -2. Basic Permissions. -All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. - -You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. - -Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. - -3. Protecting Users' Legal Rights From Anti-Circumvention Law. -No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. - -When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. - -4. Conveying Verbatim Copies. -You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. - -You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. - -5. Conveying Modified Source Versions. -You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”. - - c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. - -A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. - -6. Conveying Non-Source Forms. -You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: - - a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. - - d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. - -A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. - -A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. - -“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. - -If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). - -The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. - -Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. - -7. Additional Terms. -“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. - -When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. - -Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or authors of the material; or - - e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. - -All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. - -If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. - -Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. - -8. Termination. -You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). - -However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. - -Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. - -Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. - -9. Acceptance Not Required for Having Copies. -You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. - -10. Automatic Licensing of Downstream Recipients. -Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. - -An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. - -You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. - -11. Patents. -A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”. - -A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. - -Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. - -In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. - -If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. - -If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. - -A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. - -Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. - -12. No Surrender of Others' Freedom. -If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - -13. Use with the GNU Affero General Public License. -Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. - -14. Revised Versions of this License. -The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. - -If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. - -Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. - -15. Disclaimer of Warranty. -THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - -16. Limitation of Liability. -IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -17. Interpretation of Sections 15 and 16. -If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. - -END OF TERMS AND CONDITIONS - -How to Apply These Terms to Your New Programs - -If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. - -To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - -If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”. - -You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . - -The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . \ No newline at end of file + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/README.md b/README.md index 28b2e0ad..6c2e4771 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Aladin Lite can be imported with: ## Licence -Aladin Lite is currently licensed under GPL v3.0 +Aladin Lite is currently licensed under LGPL-3.0-or-later If you think this license might prevent you from using Aladin Lite in your pages/application/portal, please open an issue or [contact us](mailto:cds-question@unistra.fr) diff --git a/codemeta.json b/codemeta.json index 9f478f27..e5dbc9fe 100644 --- a/codemeta.json +++ b/codemeta.json @@ -1,7 +1,7 @@ { "@context": "https://doi.org/10.5063/schema/codemeta-2.0", "@type": "SoftwareSourceCode", - "license": "https://spdx.org/licenses/GPL-3.0", + "license": "https://spdx.org/licenses/LGPL-3.0-or-later", "codeRepository": "https://github.com/cds-astro/aladin-lite", "dateCreated": "2013-05-01", "datePublished": "2013-05-01", diff --git a/package.json b/package.json index 4174df5a..e453bf58 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "version": "3.9.0-beta", "description": "An astronomical HiPS visualizer in the browser", "author": "Thomas Boch and Matthieu Baumann", - "license": "GPL-3", + "license": "LGPL-3.0-or-later", "module": "./dist/aladin.js", "main": "./dist/aladin.js", "files": [ diff --git a/src/glsl/webgl2/catalogs/aitoff.vert b/src/glsl/webgl2/catalogs/aitoff.vert index 3db7f4a5..091068d8 100644 --- a/src/glsl/webgl2/catalogs/aitoff.vert +++ b/src/glsl/webgl2/catalogs/aitoff.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/arc.vert b/src/glsl/webgl2/catalogs/arc.vert index aeceff19..4bcd955c 100644 --- a/src/glsl/webgl2/catalogs/arc.vert +++ b/src/glsl/webgl2/catalogs/arc.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/catalog.frag b/src/glsl/webgl2/catalogs/catalog.frag index 9c2cacbc..86d77342 100644 --- a/src/glsl/webgl2/catalogs/catalog.frag +++ b/src/glsl/webgl2/catalogs/catalog.frag @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; in vec2 out_uv; in vec3 out_p; diff --git a/src/glsl/webgl2/catalogs/healpix.vert b/src/glsl/webgl2/catalogs/healpix.vert index 32cd3fed..c26b6f8a 100644 --- a/src/glsl/webgl2/catalogs/healpix.vert +++ b/src/glsl/webgl2/catalogs/healpix.vert @@ -1,5 +1,6 @@ #version 300 es -precision lowp float; +precision highp float; + layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/mercator.vert b/src/glsl/webgl2/catalogs/mercator.vert index 329c6674..70cc07f2 100644 --- a/src/glsl/webgl2/catalogs/mercator.vert +++ b/src/glsl/webgl2/catalogs/mercator.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/mollweide.vert b/src/glsl/webgl2/catalogs/mollweide.vert index 7ea04da8..c7c5d79b 100644 --- a/src/glsl/webgl2/catalogs/mollweide.vert +++ b/src/glsl/webgl2/catalogs/mollweide.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/ortho.frag b/src/glsl/webgl2/catalogs/ortho.frag index f4c0b370..96f711c2 100644 --- a/src/glsl/webgl2/catalogs/ortho.frag +++ b/src/glsl/webgl2/catalogs/ortho.frag @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; in vec2 out_uv; in vec3 out_p; diff --git a/src/glsl/webgl2/catalogs/ortho.vert b/src/glsl/webgl2/catalogs/ortho.vert index 7de82d72..c12ba348 100644 --- a/src/glsl/webgl2/catalogs/ortho.vert +++ b/src/glsl/webgl2/catalogs/ortho.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; diff --git a/src/glsl/webgl2/catalogs/tan.vert b/src/glsl/webgl2/catalogs/tan.vert index 69685fec..9240118f 100644 --- a/src/glsl/webgl2/catalogs/tan.vert +++ b/src/glsl/webgl2/catalogs/tan.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; diff --git a/src/glsl/webgl2/colormaps/colormap.frag b/src/glsl/webgl2/colormaps/colormap.frag index fb4671da..848dbd7f 100644 --- a/src/glsl/webgl2/colormaps/colormap.frag +++ b/src/glsl/webgl2/colormaps/colormap.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2D; +precision highp float; +precision highp sampler2D; in vec2 out_uv; out vec4 color; diff --git a/src/glsl/webgl2/colormaps/colormap.vert b/src/glsl/webgl2/colormaps/colormap.vert index 31e4f067..b97c5a5e 100644 --- a/src/glsl/webgl2/colormaps/colormap.vert +++ b/src/glsl/webgl2/colormaps/colormap.vert @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2D; +precision highp float; +precision highp sampler2D; layout (location = 0) in vec2 position; layout (location = 1) in vec2 uv; diff --git a/src/glsl/webgl2/fits/i16.frag b/src/glsl/webgl2/fits/i16.frag index 6d725d7a..526063c6 100644 --- a/src/glsl/webgl2/fits/i16.frag +++ b/src/glsl/webgl2/fits/i16.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; in vec2 frag_uv; diff --git a/src/glsl/webgl2/fits/i32.frag b/src/glsl/webgl2/fits/i32.frag index 5a240378..de5454fb 100644 --- a/src/glsl/webgl2/fits/i32.frag +++ b/src/glsl/webgl2/fits/i32.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; in vec2 frag_uv; diff --git a/src/glsl/webgl2/fits/u8.frag b/src/glsl/webgl2/fits/u8.frag index ecf7771b..326bdfd2 100644 --- a/src/glsl/webgl2/fits/u8.frag +++ b/src/glsl/webgl2/fits/u8.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; in vec2 frag_uv; diff --git a/src/glsl/webgl2/hips/rasterizer/f32.frag b/src/glsl/webgl2/hips/rasterizer/f32.frag index e14ce9f6..78f0652d 100644 --- a/src/glsl/webgl2/hips/rasterizer/f32.frag +++ b/src/glsl/webgl2/hips/rasterizer/f32.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/rasterizer/i16.frag b/src/glsl/webgl2/hips/rasterizer/i16.frag index bea6f9a9..ffd24205 100644 --- a/src/glsl/webgl2/hips/rasterizer/i16.frag +++ b/src/glsl/webgl2/hips/rasterizer/i16.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/rasterizer/i32.frag b/src/glsl/webgl2/hips/rasterizer/i32.frag index ad85c03e..1ecf379d 100644 --- a/src/glsl/webgl2/hips/rasterizer/i32.frag +++ b/src/glsl/webgl2/hips/rasterizer/i32.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/rasterizer/rgba.frag b/src/glsl/webgl2/hips/rasterizer/rgba.frag index c2a757e8..428d64fd 100644 --- a/src/glsl/webgl2/hips/rasterizer/rgba.frag +++ b/src/glsl/webgl2/hips/rasterizer/rgba.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/rasterizer/rgba2cmap.frag b/src/glsl/webgl2/hips/rasterizer/rgba2cmap.frag index f2ac06e4..f47eedbe 100644 --- a/src/glsl/webgl2/hips/rasterizer/rgba2cmap.frag +++ b/src/glsl/webgl2/hips/rasterizer/rgba2cmap.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/rasterizer/u8.frag b/src/glsl/webgl2/hips/rasterizer/u8.frag index 3370a68a..642f074b 100644 --- a/src/glsl/webgl2/hips/rasterizer/u8.frag +++ b/src/glsl/webgl2/hips/rasterizer/u8.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/raytracer/backcolor.frag b/src/glsl/webgl2/hips/raytracer/backcolor.frag index 1ca4d3cc..c2b8c78e 100644 --- a/src/glsl/webgl2/hips/raytracer/backcolor.frag +++ b/src/glsl/webgl2/hips/raytracer/backcolor.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision mediump int; +precision highp float; +precision highp int; out vec4 out_frag_color; diff --git a/src/glsl/webgl2/hips/raytracer/backcolor.vert b/src/glsl/webgl2/hips/raytracer/backcolor.vert index 770e6018..af92c810 100644 --- a/src/glsl/webgl2/hips/raytracer/backcolor.vert +++ b/src/glsl/webgl2/hips/raytracer/backcolor.vert @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision mediump int; +precision highp float; +precision highp int; layout (location = 0) in vec2 pos_clip_space; diff --git a/src/glsl/webgl2/hips/raytracer/f32.frag b/src/glsl/webgl2/hips/raytracer/f32.frag index 52e84a52..32dacda6 100644 --- a/src/glsl/webgl2/hips/raytracer/f32.frag +++ b/src/glsl/webgl2/hips/raytracer/f32.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp int; in vec3 frag_pos; in vec2 out_clip_pos; diff --git a/src/glsl/webgl2/hips/raytracer/i16.frag b/src/glsl/webgl2/hips/raytracer/i16.frag index d320278d..f7c860f1 100644 --- a/src/glsl/webgl2/hips/raytracer/i16.frag +++ b/src/glsl/webgl2/hips/raytracer/i16.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp int; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/raytracer/i32.frag b/src/glsl/webgl2/hips/raytracer/i32.frag index b6c71bc4..c2c8cc37 100644 --- a/src/glsl/webgl2/hips/raytracer/i32.frag +++ b/src/glsl/webgl2/hips/raytracer/i32.frag @@ -1,9 +1,9 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp usampler2DArray; +precision highp isampler2DArray; +precision highp int; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/raytracer/raytracer.vert b/src/glsl/webgl2/hips/raytracer/raytracer.vert index 91f2cec8..c44a21a2 100644 --- a/src/glsl/webgl2/hips/raytracer/raytracer.vert +++ b/src/glsl/webgl2/hips/raytracer/raytracer.vert @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision mediump int; +precision highp float; +precision highp int; layout (location = 0) in vec2 pos_clip_space; layout (location = 1) in vec3 pos_world_space; diff --git a/src/glsl/webgl2/hips/raytracer/rgba.frag b/src/glsl/webgl2/hips/raytracer/rgba.frag index 2abe3c45..94c0ce70 100644 --- a/src/glsl/webgl2/hips/raytracer/rgba.frag +++ b/src/glsl/webgl2/hips/raytracer/rgba.frag @@ -1,7 +1,7 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp int; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips/raytracer/rgba2cmap.frag b/src/glsl/webgl2/hips/raytracer/rgba2cmap.frag index 8251fe7e..ef05ff07 100644 --- a/src/glsl/webgl2/hips/raytracer/rgba2cmap.frag +++ b/src/glsl/webgl2/hips/raytracer/rgba2cmap.frag @@ -1,9 +1,9 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp sampler2DArray; +precision highp isampler2DArray; +precision highp int; in vec3 frag_pos; in vec2 out_clip_pos; diff --git a/src/glsl/webgl2/hips/raytracer/u8.frag b/src/glsl/webgl2/hips/raytracer/u8.frag index b965b68c..f233827c 100644 --- a/src/glsl/webgl2/hips/raytracer/u8.frag +++ b/src/glsl/webgl2/hips/raytracer/u8.frag @@ -1,9 +1,9 @@ #version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp usampler2DArray; +precision highp isampler2DArray; +precision highp int; uniform sampler2DArray tex; diff --git a/src/glsl/webgl2/hips3d/f32.frag b/src/glsl/webgl2/hips3d/f32.frag index 7850ffd0..404653ce 100644 --- a/src/glsl/webgl2/hips3d/f32.frag +++ b/src/glsl/webgl2/hips3d/f32.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler3D; +precision highp float; +precision highp sampler3D; uniform sampler3D tex; diff --git a/src/glsl/webgl2/hips3d/i16.frag b/src/glsl/webgl2/hips3d/i16.frag index 1b4e1de0..c9040e12 100644 --- a/src/glsl/webgl2/hips3d/i16.frag +++ b/src/glsl/webgl2/hips3d/i16.frag @@ -1,8 +1,8 @@ #version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; uniform sampler3D tex; diff --git a/src/glsl/webgl2/hips3d/i32.frag b/src/glsl/webgl2/hips3d/i32.frag index a32bc730..a5ca26f0 100644 --- a/src/glsl/webgl2/hips3d/i32.frag +++ b/src/glsl/webgl2/hips3d/i32.frag @@ -1,8 +1,8 @@ #version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; uniform sampler3D tex; diff --git a/src/glsl/webgl2/hips3d/raster.vert b/src/glsl/webgl2/hips3d/raster.vert index 1802a43d..e18ed10b 100644 --- a/src/glsl/webgl2/hips3d/raster.vert +++ b/src/glsl/webgl2/hips3d/raster.vert @@ -1,5 +1,5 @@ #version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 lonlat; layout (location = 1) in vec3 uv; diff --git a/src/glsl/webgl2/hips3d/red.frag b/src/glsl/webgl2/hips3d/red.frag index 6fe851bb..16548b17 100644 --- a/src/glsl/webgl2/hips3d/red.frag +++ b/src/glsl/webgl2/hips3d/red.frag @@ -1,8 +1,8 @@ #version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; uniform sampler3D tex; diff --git a/src/glsl/webgl2/hips3d/u8.frag b/src/glsl/webgl2/hips3d/u8.frag index cda29d1d..be8c4178 100644 --- a/src/glsl/webgl2/hips3d/u8.frag +++ b/src/glsl/webgl2/hips3d/u8.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; -precision lowp sampler3D; +precision highp float; +precision highp sampler3D; uniform sampler3D tex; diff --git a/src/glsl/webgl2/moc/base.frag b/src/glsl/webgl2/moc/base.frag index f2ff4389..6be78dfc 100644 --- a/src/glsl/webgl2/moc/base.frag +++ b/src/glsl/webgl2/moc/base.frag @@ -1,6 +1,6 @@ #version 300 es -precision lowp float; +precision highp float; out vec4 color; uniform vec4 u_color; diff --git a/src/glsl/webgl2/passes/post_fragment_100es.frag b/src/glsl/webgl2/passes/post_fragment_100es.frag index 2530a88b..03d3bc63 100644 --- a/src/glsl/webgl2/passes/post_fragment_100es.frag +++ b/src/glsl/webgl2/passes/post_fragment_100es.frag @@ -1,5 +1,5 @@ #version 300 es -precision mediump float; +precision highp float; in vec2 v_tc; out vec4 color; diff --git a/src/glsl/webgl2/passes/post_vertex_100es.vert b/src/glsl/webgl2/passes/post_vertex_100es.vert index 1078e0ad..5657aff2 100644 --- a/src/glsl/webgl2/passes/post_vertex_100es.vert +++ b/src/glsl/webgl2/passes/post_vertex_100es.vert @@ -1,5 +1,5 @@ #version 300 es -precision mediump float; +precision highp float; layout (location = 0) in vec2 a_pos; out vec2 v_tc; From 58ffb782ccbb24c564aa8ecb447a36604dd959be Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Thu, 28 May 2026 20:10:37 +0200 Subject: [PATCH 14/17] optimization preserveDrawingBuffer: false --- src/core/Cargo.toml | 2 +- src/core/al-api/Cargo.toml | 2 +- src/core/al-core/Cargo.toml | 2 +- src/core/al-core/src/webgl_ctx.rs | 9 +- src/core/src/shaders.rs | 10795 ++++++++++++++-------------- src/js/Aladin.js | 2 +- src/js/View.js | 4 + 7 files changed, 5410 insertions(+), 5406 deletions(-) diff --git a/src/core/Cargo.toml b/src/core/Cargo.toml index 81184d93..6c5dfe57 100644 --- a/src/core/Cargo.toml +++ b/src/core/Cargo.toml @@ -3,7 +3,7 @@ name = "aladin-lite" description = "Aladin Lite v3 introduces a new graphical engine written in Rust with the use of WebGL" license = "BSD-3-Clause" repository = "https://github.com/cds-astro/aladin-lite" -version = "3.8.2" +version = "3.9.0" authors = [ "baumannmatthieu0@gmail.com", "matthieu.baumann@astro.unistra.fr",] edition = "2018" diff --git a/src/core/al-api/Cargo.toml b/src/core/al-api/Cargo.toml index 3f3fb422..3776fa9d 100644 --- a/src/core/al-api/Cargo.toml +++ b/src/core/al-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "al-api" -version = "3.8.2" +version = "3.9.0" authors = ["baumannmatthieu0@gmail.com", "matthieu.baumann@astro.unistra.fr"] edition = "2018" diff --git a/src/core/al-core/Cargo.toml b/src/core/al-core/Cargo.toml index d9d5b62c..e7887fef 100644 --- a/src/core/al-core/Cargo.toml +++ b/src/core/al-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "al-core" -version = "3.8.2" +version = "3.9.0" authors = ["baumannmatthieu0@gmail.com", "matthieu.baumann@astro.unistra.fr"] edition = "2018" diff --git a/src/core/al-core/src/webgl_ctx.rs b/src/core/al-core/src/webgl_ctx.rs index 89c2dd0b..34f5a3cb 100644 --- a/src/core/al-core/src/webgl_ctx.rs +++ b/src/core/al-core/src/webgl_ctx.rs @@ -29,16 +29,15 @@ impl WebGlContext { .dyn_into::() .unwrap_abort(); - // See https://stackoverflow.com/a/26790802/13456997 - // preserveDrawingBuffer enabled for exporting the view as a PNG - let context_options = - js_sys::JSON::parse("{\"antialias\":false, \"preserveDrawingBuffer\": true}")?; + //let context_options = js_sys::JSON::parse("{}")?; + //let context_options = + // js_sys::JSON::parse("{\"antialias\":false}")?; //js_sys::JSON::parse("{\"antialias\":false}")?; #[cfg(feature = "webgl2")] let gl = Rc::new( canvas - .get_context_with_context_options("webgl2", context_options.as_ref())? + .get_context("webgl2")? .unwrap_abort() .dyn_into::() .unwrap_abort(), diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index 7f3bc81c..f305194d 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -3,1786 +3,35 @@ use std::collections::HashMap; pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); out.insert( - r"line_base.frag", - r#"#version 300 es - -precision highp float; - -out vec4 color; -in vec2 l; - -uniform vec4 u_color; -uniform float u_thickness; -uniform float u_width; -uniform float u_height; - -void main() { - if (l.x > 0.05) { - discard; - } else { - color = u_color; - - float dist = abs((u_thickness + 2.0) * l.y); - - float half_thickness = (u_thickness + 2.0) * 0.5; - color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); - } -}"#, - ); - out.insert( - r"fits_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i16(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2cmap_rgba(frag_uv_start); - vec4 color_end = uvw2cmap_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"catalogs_healpix.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_healpix(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips3d_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i16(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.vert", - r#"#version 300 es -precision lowp float; -precision mediump int; - -layout (location = 0) in vec2 pos_clip_space; - -uniform vec2 ndc_to_clip; -uniform float czf; - -void main() { - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); -}"#, - ); - out.insert( - r"image_sampler.frag", + r"line_inst_ndc.vert", r#"#version 300 es precision highp float; -precision highp sampler2D; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -void main() { - out_frag_color = texture(tex, frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"line_inst_lonlat.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; layout (location = 2) in vec2 vertex; -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - out vec2 l; -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} +uniform float u_width; +uniform float u_height; +uniform float u_thickness; void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); - - vec2 da = p_a_clip - p_b_clip; - - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; + vec2 x_b = p_b - p_a; vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); float ndc2pix = 2.0 / u_width; - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); -}"#, - ); - out.insert( - r"hips_raytracer_i16.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i16(uv); - - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision lowp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"hips_rasterizer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_i32(uv0); - vec4 color_end = uvw2c_i32(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"passes_post_vertex_100es.vert", - r#"#version 300 es -precision mediump float; - -layout (location = 0) in vec2 a_pos; -out vec2 v_tc; - -void main() { - gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); - v_tc = a_pos; + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); }"#, ); out.insert( r"hips_rasterizer_f32.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; @@ -2050,33 +299,175 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_f32.frag", + r"catalogs_aitoff.vert", r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; +uniform float current_time; +uniform mat3 inv_model; -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; -uniform Tile textures_tiles[12]; +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_aitoff(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_raytracer_raytracer.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 pos_clip_space; +layout (location = 1) in vec3 pos_world_space; + +out vec2 out_clip_pos; +out vec3 frag_pos; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform mat3 model; + +void main() { + vec2 uv = pos_clip_space * 0.5 + 0.5; + + frag_pos = model * pos_world_space; + + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); + out_clip_pos = pos_clip_space; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; -uniform float opacity; uniform sampler2DArray tex; -struct TileColor { - Tile tile; - vec4 color; - bool found; -}; +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; uniform float scale; uniform float offset; @@ -2319,141 +710,633 @@ vec4 uvw2c_u8(vec3 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} +void main() { + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, ); + out.insert( + r"image_sampler.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +void main() { + out_frag_color = texture(tex, frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_u8.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); } -HashDxDy hash_with_dxdy(int depth, vec3 p) { +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; } - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); + return value; } -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} - return vec3(offset, float(tile.texture_idx)); + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); } void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - + vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - vec4 c = uvw2c_f32(uv); - - out_frag_color = c; + out_frag_color = uv2c_u8(frag_uv); out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; + +out vec3 frag_uv; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv = uv; +}"#, + ); + out.insert( + r"hips_rasterizer_rgba2cmap.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; + +uniform sampler2DArray tex; + +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec4 color_start = uvw2cmap_rgba(frag_uv_start); + vec4 color_end = uvw2cmap_rgba(frag_uv_end); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; }"#, ); out.insert( r"hips_rasterizer_i16.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; +precision highp float; +precision highp sampler2DArray; uniform sampler2DArray tex; @@ -2718,792 +1601,14 @@ void main() { out_frag_color = mix(color_start, color_end, frag_blending_factor); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_u8(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_red.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = vec3(frag_uv.xyz); - vec4 color = uvw2c_ra(uv); - - out_frag_color = color; - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"hips3d_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_u8(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); -}"#, - ); - out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision mediump float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - }"#, ); out.insert( r"fits_i32.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2D; -precision mediump int; +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; in vec2 frag_uv; @@ -3909,35 +2014,1969 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_raytracer.vert", + r"passes_post_vertex_100es.vert", r#"#version 300 es -precision lowp float; -precision mediump int; +precision highp float; -layout (location = 0) in vec2 pos_clip_space; -layout (location = 1) in vec3 pos_world_space; - -out vec2 out_clip_pos; -out vec3 frag_pos; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform mat3 model; +layout (location = 0) in vec2 a_pos; +out vec2 v_tc; void main() { - vec2 uv = pos_clip_space * 0.5 + 0.5; + gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0); + v_tc = a_pos; +}"#, + ); + out.insert( + r"hips3d_i32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; - frag_pos = model * pos_world_space; +uniform sampler3D tex; - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); - out_clip_pos = pos_clip_space; +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"image_base.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"hips3d_i16.frag", + r#"#version 300 es +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i16(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"line_base.frag", + r#"#version 300 es + +precision highp float; + +out vec4 color; +in vec2 l; + +uniform vec4 u_color; +uniform float u_thickness; +uniform float u_width; +uniform float u_height; + +void main() { + if (l.x > 0.05) { + discard; + } else { + color = u_color; + + float dist = abs((u_thickness + 2.0) * l.y); + + float half_thickness = (u_thickness + 2.0) * 0.5; + color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); + } +}"#, + ); + out.insert( + r"hips_raytracer_i32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; +precision highp usampler2DArray; +precision highp isampler2DArray; +precision highp int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i32(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"line_inst_lonlat.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; +layout (location = 2) in vec2 vertex; + +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; + +out vec2 l; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); + + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); +}"#, + ); + out.insert( + r"hips_raytracer_i16.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; +precision highp int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i16(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_base.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"hips_raytracer_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; +precision highp int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +struct TileColor { + Tile tile; + vec4 color; + bool found; +}; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_f32(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( r"hips_raytracer_backcolor.frag", r#"#version 300 es -precision lowp float; -precision mediump int; +precision highp float; +precision highp int; out vec4 out_frag_color; @@ -3948,16 +3987,34 @@ void main() { }"#, ); out.insert( - r"colormaps_colormap.frag", + r"hips_raytracer_rgba.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2D; +precision highp float; +precision highp sampler2DArray; +precision highp int; -in vec2 out_uv; -out vec4 color; +uniform sampler2DArray tex; -uniform sampler2D texture_fbo; -uniform float alpha; +in vec2 out_clip_pos; +in vec3 frag_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; uniform sampler2D colormaps; uniform float num_colormaps; @@ -3967,20 +4024,505 @@ vec4 colormap_f(float x) { float id = (colormap_id + 0.5) / num_colormaps; return texture(colormaps, vec2(x, id)); } +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +uniform float opacity; +uniform vec4 no_tile_color; void main() { - float opacity = texture(texture_fbo, out_uv).r; + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2c_rgba(uv); - float o = smoothstep(0.0, 0.1, opacity); - - color = colormap_f(opacity); - color.a = o * alpha; + out_frag_color = c; + out_frag_color = vec4(c.rgb, opacity * c.a); }"#, ); out.insert( - r"catalogs_aitoff.vert", + r"catalogs_catalog.frag", r#"#version 300 es -precision lowp float; +precision highp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"moc_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 lonlat; + +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.f, 1.f); +}"#, + ); + out.insert( + r"catalogs_tan.vert", + r#"#version 300 es +precision highp float; + layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; @@ -4096,35 +4638,23 @@ vec2 proj(vec3 p) { } } + void main() { vec3 p = inv_model * center; - vec2 center_pos_clip_space = world2clip_aitoff(p); + vec2 center_pos_clip_space = world2clip_gnomonic(p); vec2 pos_clip_space = center_pos_clip_space; gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); out_uv = uv; out_p = p; -}"#, - ); - out.insert( - r"moc_base.frag", - r#"#version 300 es - -precision lowp float; -out vec4 color; - -uniform vec4 u_color; - -void main() { - color = u_color; }"#, ); out.insert( r"catalogs_mercator.vert", r#"#version 300 es -precision lowp float; +precision highp float; layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; layout (location = 2) in vec3 center; @@ -4254,26 +4784,1818 @@ void main() { }"#, ); out.insert( - r"colormaps_colormap.vert", + r"hips_rasterizer_u8.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2D; +precision highp float; +precision highp sampler2DArray; -layout (location = 0) in vec2 position; -layout (location = 1) in vec2 uv; +uniform sampler2DArray tex; -out vec2 out_uv; +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; void main() { - gl_Position = vec4(position, 0.f, 1.f); + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_u8(uv0); + vec4 color_end = uvw2c_u8(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_ortho.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_orthographic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_arc.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_arc(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); +}"#, + ); + out.insert( + r"hips3d_u8.frag", + r#"#version 300 es +precision highp float; +precision highp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_u8(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_rgba2cmap.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; +precision highp sampler2DArray; +precision highp isampler2DArray; +precision highp int; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; +uniform sampler2DArray tex; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2cmap_rgba(uv); + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"moc_base.frag", + r#"#version 300 es + +precision highp float; +out vec4 color; + +uniform vec4 u_color; + +void main() { + color = u_color; +}"#, + ); + out.insert( + r"fits_i16.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i16(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_healpix.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_healpix(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; }"#, ); out.insert( r"hips3d_f32.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler3D; +precision highp float; +precision highp sampler3D; uniform sampler3D tex; @@ -4536,145 +6858,338 @@ void main() { }"#, ); out.insert( - r"catalogs_ortho.vert", + r"catalogs_ortho.frag", r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; +precision highp float; -uniform float current_time; -uniform mat3 inv_model; +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 pos_clip_space; uniform vec2 ndc_to_clip; uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - void main() { - vec3 p = inv_model * center; + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); +}"#, + ); + out.insert( + r"hips_rasterizer_i32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; - vec2 center_pos_clip_space = world2clip_orthographic(p); +uniform sampler2DArray tex; - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_i32(uv0); + vec4 color_end = uvw2c_i32(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision highp float; +precision highp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); out_uv = uv; - out_p = p; }"#, ); out.insert( r"hips_raytracer_u8.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; +precision highp float; +precision highp sampler2DArray; +precision highp usampler2DArray; +precision highp isampler2DArray; +precision highp int; uniform sampler2DArray tex; @@ -5065,1112 +7580,16 @@ void main() { }"#, ); out.insert( - r"image_base.vert", + r"colormaps_colormap.frag", r#"#version 300 es precision highp float; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp sampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_catalog.frag", - r#"#version 300 es -precision lowp float; +precision highp sampler2D; in vec2 out_uv; -in vec3 out_p; - out vec4 color; -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"fits_base.vert", - r#"#version 300 es -precision highp float; -precision highp int; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; - -out vec3 frag_uv; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; -}"#, - ); - out.insert( - r"catalogs_mollweide.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mollweide(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"moc_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 lonlat; - -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); -}"#, - ); - out.insert( - r"hips_rasterizer_u8.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; +uniform sampler2D texture_fbo; +uniform float alpha; uniform sampler2D colormaps; uniform float num_colormaps; @@ -6180,1077 +7599,30 @@ vec4 colormap_f(float x) { float id = (colormap_id + 0.5) / num_colormaps; return texture(colormaps, vec2(x, id)); } -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; + float opacity = texture(texture_fbo, out_uv).r; - vec4 color_start = uvw2c_u8(uv0); - vec4 color_end = uvw2c_u8(uv1); + float o = smoothstep(0.0, 0.1, opacity); - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; + color = colormap_f(opacity); + color.a = o * alpha; }"#, ); out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision lowp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"line_inst_ndc.vert", + r"hips3d_red.frag", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; -layout (location = 2) in vec2 vertex; - -out vec2 l; - -uniform float u_width; -uniform float u_height; -uniform float u_thickness; - -void main() { - vec2 x_b = p_b - p_a; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float current_time; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); -}"#, - ); - out.insert( - r"catalogs_tan.vert", - r#"#version 300 es -precision lowp float; - -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_gnomonic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_raytracer_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision lowp usampler2DArray; -precision lowp isampler2DArray; -precision mediump int; - -uniform sampler2DArray tex; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; - -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i32(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_i32.frag", - r#"#version 300 es -precision lowp float; -precision lowp sampler3D; -precision lowp isampler3D; -precision lowp usampler3D; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; uniform sampler3D tex; in vec3 frag_uv; out vec4 out_frag_color; +uniform float opacity; uniform float scale; uniform float offset; @@ -7494,409 +7866,38 @@ vec4 uvw2c_u8(vec3 uv) { return val2c(val); } -uniform float opacity; - void main() { vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_i32(uv); + vec4 color = uvw2c_ra(uv); out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; + out_frag_color.a = opacity * out_frag_color.a; }"#, ); out.insert( - r"hips_raytracer_rgba.frag", + r"passes_post_fragment_100es.frag", r#"#version 300 es -precision lowp float; -precision lowp sampler2DArray; -precision mediump int; +precision highp float; -uniform sampler2DArray tex; +in vec2 v_tc; +out vec4 color; -in vec2 out_clip_pos; -in vec3 frag_pos; -out vec4 out_frag_color; +uniform sampler2D fbo_tex; -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); } -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); } -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -uniform float opacity; -uniform vec4 no_tile_color; - void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2c_rgba(uv); + color = texture(fbo_tex, v_tc); - out_frag_color = c; - out_frag_color = vec4(c.rgb, opacity * c.a); }"#, ); out diff --git a/src/js/Aladin.js b/src/js/Aladin.js index a449e73d..61b18a0b 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -2211,7 +2211,7 @@ export let Aladin = (function () { * view in the counter clockwise order (or towards the east) */ Aladin.prototype.setRotation = function (rotation) { - if (Utils.isNumber(rotation)) { + if (!Utils.isNumber(rotation)) { console.warn("Rotation angle is not valid:", rotation) return; } diff --git a/src/js/View.js b/src/js/View.js index 48780e8e..7bd903b1 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -581,6 +581,10 @@ export let View = (function () { }; View.prototype.getRawPixelsCanvas = function(width, height) { + // important: redraw must be called just before getting the pixels + // because the drawing buffer is not preserved (preserveDrawingBuffer = false) + this.redraw() + const canvas = this.wasm.canvas(); const c = document.createElement('canvas'); From 21e52325e92f3b4c9736c7c813f60beaa2de88f1 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Sun, 31 May 2026 14:58:50 +0200 Subject: [PATCH 15/17] fix #376: detect localhost urls and proxyfy them --- src/core/al-core/src/image/fits.rs | 10 ++++++ src/core/src/app.rs | 49 +++++++++++++++++++----------- src/core/src/renderable/mod.rs | 9 +++--- src/js/vo/samp.js | 24 ++++++++++----- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/core/al-core/src/image/fits.rs b/src/core/al-core/src/image/fits.rs index 4bd7f9ed..4cc2a84e 100644 --- a/src/core/al-core/src/image/fits.rs +++ b/src/core/al-core/src/image/fits.rs @@ -13,6 +13,8 @@ use std::io::Cursor; use std::ops::Range; use wasm_bindgen::JsValue; +use fitsrs::hdu::header::ValueMap; + #[derive(Debug)] pub struct FitsImage<'a> { // Margin values for HiPS3D cubic tiles @@ -37,6 +39,9 @@ pub struct FitsImage<'a> { pub data_byte_offset: Range, // raw bytes of the data image (in Big-Endian) pub raw_bytes: Cow<'a, [u8]>, + + // keep the header keywords and their values + pub header: ValueMap } impl<'a> FitsImage<'a> { @@ -76,6 +81,7 @@ impl<'a> FitsImage<'a> { let wcs = hdu.wcs().ok(); + let values: &ValueMap = &*header; images.push(Self { trim1, trim2, @@ -90,6 +96,7 @@ impl<'a> FitsImage<'a> { blank, data_byte_offset, raw_bytes, + header: values.clone() }); } } @@ -149,6 +156,8 @@ impl<'a> FitsImage<'a> { }; if let Some(raw_bytes) = raw_bytes { + let values: &ValueMap = &*header; + images.push(Self { trim1, trim2, @@ -163,6 +172,7 @@ impl<'a> FitsImage<'a> { blank, data_byte_offset, raw_bytes: Cow::Owned(raw_bytes), + header: values.clone() }); } } diff --git a/src/core/src/app.rs b/src/core/src/app.rs index 0e27f66e..826b8078 100644 --- a/src/core/src/app.rs +++ b/src/core/src/app.rs @@ -1176,7 +1176,7 @@ impl App { layer: String, bytes: &[u8], wcs: WCS, - cfg: ImageMetadata, + options: ImageMetadata, ) -> Result { let gl = self.gl.clone(); @@ -1188,7 +1188,7 @@ impl App { images: vec![image], id: layer.clone(), layer, - meta: cfg, + options, }; let params = layer.get_params(); @@ -1212,9 +1212,11 @@ impl App { pub(crate) fn add_fits_image( &mut self, bytes: &[u8], - meta: ImageMetadata, + options: ImageMetadata, layer: String, ) -> Result { + use fitsrs::hdu::header::ValueMap; + let gl = self.gl.clone(); // Stop the current inertia // And disable it while the fits has not been loaded @@ -1225,19 +1227,20 @@ impl App { let gz = fitsrs::gz::GzReader::new(Cursor::new(bytes)) .map_err(|_| JsValue::from_str("Error creating gz wrapper"))?; - let parse_fits_images_from_bytes = |raw_bytes: &[u8]| -> Result, JsValue> { - Ok(FitsImage::from_raw_bytes(raw_bytes)? + let parse_fits_images_from_bytes = |raw_bytes: &[u8]| -> Result<(Vec, Vec), JsValue> { + let (images, headers) = FitsImage::from_raw_bytes(raw_bytes)? .into_iter() .filter_map( |FitsImage { - bitpix, - bscale, - bzero, - blank, - wcs, - raw_bytes, - .. - }| { + bitpix, + bscale, + bzero, + blank, + wcs, + raw_bytes, + header, + .. + }| { if let Some(wcs) = wcs { let image = Image::from_fits_hdu( &gl, @@ -1250,16 +1253,18 @@ impl App { camera_coo_sys, ) .ok()?; - Some(image) + Some((image, header)) } else { None } }, ) - .collect::>()) + .collect::<(Vec<_>, Vec<_>)>(); + + Ok((images, headers)) }; - let images = match gz { + let (images, headers) = match gz { fitsrs::gz::GzReader::GzReader(bytes) => parse_fits_images_from_bytes(bytes.get_ref())?, fitsrs::gz::GzReader::Reader(bytes) => parse_fits_images_from_bytes(bytes.get_ref())?, }; @@ -1267,12 +1272,13 @@ impl App { if images.is_empty() { Err(JsValue::from_str("no images have been parsed")) } else { + let layer = ImageLayer { images, id: layer.clone(), layer, - meta, + options, }; let params = layer.get_params(); @@ -1284,7 +1290,14 @@ impl App { )?; self.request_redraw = true; - let promise = js_sys::Promise::resolve(&serde_wasm_bindgen::to_value(¶ms)?); + let obj: js_sys::Object = serde_wasm_bindgen::to_value(¶ms)? + .dyn_into()?; + + use std::iter::FromIterator; + let arr = js_sys::Array::from_iter(headers.iter().map(|header| serde_wasm_bindgen::to_value(&header).unwrap())); + js_sys::Reflect::set(&obj, &"headers".into(), &arr).unwrap(); + + let promise = js_sys::Promise::resolve(&obj.into()); Ok(promise) } } diff --git a/src/core/src/renderable/mod.rs b/src/core/src/renderable/mod.rs index 81963ab8..d35dca92 100644 --- a/src/core/src/renderable/mod.rs +++ b/src/core/src/renderable/mod.rs @@ -103,8 +103,8 @@ pub struct ImageLayer { pub layer: String, pub id: String, pub images: Vec, - /// Its color - pub meta: ImageMetadata, + /// Display options + pub options: ImageMetadata, } impl ImageLayer { @@ -464,7 +464,8 @@ impl Layers { layer, id, images, - meta, + options, + .. } = image; // 1. Add the layer name @@ -479,7 +480,7 @@ impl Layers { self.layers.insert(idx, layer.to_string()); // 2. Add the meta information of the layer - self.meta.insert(layer.clone(), meta); + self.meta.insert(layer.clone(), options); // 3. Add the fits image // The layer does not already exist diff --git a/src/js/vo/samp.js b/src/js/vo/samp.js index 1e69c505..6bc04bc5 100644 --- a/src/js/vo/samp.js +++ b/src/js/vo/samp.js @@ -48,6 +48,11 @@ export class SAMPConnector { let callHandler = cc.callHandler; this.cc = cc; + let isLocalhost = (url) => { + const { hostname } = new URL(url); + return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1"; + }; + let self = this; // listen for hub deconnexion/shutdown and unregister if so callHandler["samp.hub.event.shutdown"] = function(senderId, message, isCall) { @@ -91,13 +96,12 @@ export class SAMPConnector { let params = message["samp.params"]; let id = params['table-id']; - let url = params['url']; + let origUrl = params['url']; + let finalUrl = isLocalhost(origUrl) ? cc.connection.translateUrl(origUrl) : origUrl; let name = params['name'] || id; - console.log(id, url, name) - A.catalogFromURL( - url, + finalUrl, {name, onClick: 'showTable'}, // Add the catalog if the query has succeded (catalog) => { @@ -120,11 +124,13 @@ export class SAMPConnector { let params = message["samp.params"]; let id = params['table-id']; - let url = params['url']; + let origUrl = params['url']; + let finalUrl = isLocalhost(origUrl) ? cc.connection.translateUrl(origUrl) : origUrl; + let rowList = params['row-list']; // search for the catalog - let catalog = selectCatalog(id, url) + let catalog = selectCatalog(id, finalUrl) if (catalog) { let objects = []; @@ -140,11 +146,13 @@ export class SAMPConnector { let params = message["samp.params"]; let id = params['table-id']; - let url = params['url']; + let origUrl = params['url']; + let finalUrl = isLocalhost(origUrl) ? cc.connection.translateUrl(origUrl) : origUrl; + let row = params['row']; // search for the catalog - let catalog = selectCatalog(id, url) + let catalog = selectCatalog(id, finalUrl) if (catalog) { const source = catalog.sources[row]; From 47c9c1359f8c66af4df185ab621c068396708d03 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Mon, 1 Jun 2026 09:33:51 +0200 Subject: [PATCH 16/17] fix: More... replace the same layer instead of adding one --- src/js/gui/Box/StackBox.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/gui/Box/StackBox.js b/src/js/gui/Box/StackBox.js index 5eab54a8..49032464 100644 --- a/src/js/gui/Box/StackBox.js +++ b/src/js/gui/Box/StackBox.js @@ -973,10 +973,10 @@ export class OverlayStackBox extends Box { aladin.hipsBrowser = new HiPSBrowserBox(aladin); } - let newLayer = Utils.uuidv4(); - + let newLayer = hips.layer; aladin.hipsBrowser._show({ selected: (hips) => { + self.aladin.setOverlayImageLayer(hips, newLayer); }, position: { anchor: "center center" } From aba352fdfdd4b31c3e6ecf7f05ae949f11b9a4a6 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Mon, 1 Jun 2026 10:08:38 +0200 Subject: [PATCH 17/17] remove COPYING.LESSER redundant with LICENSE file --- COPYING.LESSER | 304 -- examples/al-badbaselayer.html | 4 +- src/core/al-core/src/image/fits.rs | 10 +- src/core/src/app.rs | 83 +- src/core/src/shaders.rs | 7374 ++++++++++++++-------------- src/js/HiPSProperties.js | 5 + 6 files changed, 3742 insertions(+), 4038 deletions(-) delete mode 100644 COPYING.LESSER diff --git a/COPYING.LESSER b/COPYING.LESSER deleted file mode 100644 index 51ce144d..00000000 --- a/COPYING.LESSER +++ /dev/null @@ -1,304 +0,0 @@ -GNU LESSER GENERAL PUBLIC LICENSE -Version 3, 29 June 2007 - -Copyright (C) 2007 Free Software Foundation, Inc. - -Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - -This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. - -0. Additional Definitions. - -As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. - -"The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. - -An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. - -A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". - -The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. - -The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. - -1. Exception to Section 3 of the GNU GPL. -You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. - -2. Conveying Modified Versions. -If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: - - a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. - -3. Object Code Incorporating Material from Library Header Files. -The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license document. - -4. Combined Works. -You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: - - a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license document. - - c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. - - e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) - -5. Combined Libraries. -You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. - -6. Revised Versions of the GNU Lesser General Public License. -The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. - -If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. - -GNU GENERAL PUBLIC LICENSE -Version 3, 29 June 2007 - -Copyright © 2007 Free Software Foundation, Inc. - -Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - -Preamble - -The GNU General Public License is a free, copyleft license for software and other kinds of works. - -The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. - -When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - -To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. - -For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. - -Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. - -For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. - -Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. - -Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. - -The precise terms and conditions for copying, distribution and modification follow. - -TERMS AND CONDITIONS - -0. Definitions. - -“This License” refers to version 3 of the GNU General Public License. - -“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. - -“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. - -To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work. - -A “covered work” means either the unmodified Program or a work based on the Program. - -To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. - -To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. - -An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. - -1. Source Code. -The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work. - -A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. - -The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. - -The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. - -The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. - -The Corresponding Source for a work in source code form is that same work. - -2. Basic Permissions. -All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. - -You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. - -Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. - -3. Protecting Users' Legal Rights From Anti-Circumvention Law. -No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. - -When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. - -4. Conveying Verbatim Copies. -You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. - -You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. - -5. Conveying Modified Source Versions. -You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”. - - c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. - -A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. - -6. Conveying Non-Source Forms. -You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: - - a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. - - d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. - -A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. - -A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. - -“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. - -If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). - -The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. - -Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. - -7. Additional Terms. -“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. - -When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. - -Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or authors of the material; or - - e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. - -All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. - -If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. - -Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. - -8. Termination. -You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). - -However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. - -Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. - -Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. - -9. Acceptance Not Required for Having Copies. -You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. - -10. Automatic Licensing of Downstream Recipients. -Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. - -An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. - -You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. - -11. Patents. -A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”. - -A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. - -Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. - -In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. - -If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. - -If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. - -A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. - -Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. - -12. No Surrender of Others' Freedom. -If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - -13. Use with the GNU Affero General Public License. -Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. - -14. Revised Versions of this License. -The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. - -If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. - -Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. - -15. Disclaimer of Warranty. -THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - -16. Limitation of Liability. -IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -17. Interpretation of Sections 15 and 16. -If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. - -END OF TERMS AND CONDITIONS - -How to Apply These Terms to Your New Programs - -If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. - -To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - -If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”. - -You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . - -The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . \ No newline at end of file diff --git a/examples/al-badbaselayer.html b/examples/al-badbaselayer.html index 72aecdd4..1f392ef7 100644 --- a/examples/al-badbaselayer.html +++ b/examples/al-badbaselayer.html @@ -9,8 +9,8 @@ A.init.then(() => { let aladin = A.aladin('#aladin-lite-div', {survey: ["lksldf", 'ljkjsdf'], target: "23 28 32.46 -00 10 38.9", fov: 4, projection: "AIT"}); - let hsc = aladin.newImageSurvey("P/HSC/DR2/deep/g", {colormap:"Purples", imgFormat: "fits"}); - aladin.setBaseImageLayer(hsc); + //let hsc = aladin.newImageSurvey("P/HSC/DR2/deep/g", {colormap:"Purples", imgFormat: "fits"}); + //aladin.setBaseImageLayer(hsc); }); diff --git a/src/core/al-core/src/image/fits.rs b/src/core/al-core/src/image/fits.rs index 4cc2a84e..f63ebb7c 100644 --- a/src/core/al-core/src/image/fits.rs +++ b/src/core/al-core/src/image/fits.rs @@ -41,7 +41,7 @@ pub struct FitsImage<'a> { pub raw_bytes: Cow<'a, [u8]>, // keep the header keywords and their values - pub header: ValueMap + pub header: ValueMap, } impl<'a> FitsImage<'a> { @@ -81,7 +81,7 @@ impl<'a> FitsImage<'a> { let wcs = hdu.wcs().ok(); - let values: &ValueMap = &*header; + let values: &ValueMap = header; images.push(Self { trim1, trim2, @@ -96,7 +96,7 @@ impl<'a> FitsImage<'a> { blank, data_byte_offset, raw_bytes, - header: values.clone() + header: values.clone(), }); } } @@ -156,7 +156,7 @@ impl<'a> FitsImage<'a> { }; if let Some(raw_bytes) = raw_bytes { - let values: &ValueMap = &*header; + let values: &ValueMap = header; images.push(Self { trim1, @@ -172,7 +172,7 @@ impl<'a> FitsImage<'a> { blank, data_byte_offset, raw_bytes: Cow::Owned(raw_bytes), - header: values.clone() + header: values.clone(), }); } } diff --git a/src/core/src/app.rs b/src/core/src/app.rs index 826b8078..a46c67be 100644 --- a/src/core/src/app.rs +++ b/src/core/src/app.rs @@ -1227,42 +1227,43 @@ impl App { let gz = fitsrs::gz::GzReader::new(Cursor::new(bytes)) .map_err(|_| JsValue::from_str("Error creating gz wrapper"))?; - let parse_fits_images_from_bytes = |raw_bytes: &[u8]| -> Result<(Vec, Vec), JsValue> { - let (images, headers) = FitsImage::from_raw_bytes(raw_bytes)? - .into_iter() - .filter_map( - |FitsImage { - bitpix, - bscale, - bzero, - blank, - wcs, - raw_bytes, - header, - .. - }| { - if let Some(wcs) = wcs { - let image = Image::from_fits_hdu( - &gl, - wcs, - bitpix, - raw_bytes.as_ref(), - bscale, - bzero, - blank, - camera_coo_sys, - ) - .ok()?; - Some((image, header)) - } else { - None - } - }, - ) - .collect::<(Vec<_>, Vec<_>)>(); - - Ok((images, headers)) - }; + let parse_fits_images_from_bytes = + |raw_bytes: &[u8]| -> Result<(Vec, Vec), JsValue> { + let (images, headers) = FitsImage::from_raw_bytes(raw_bytes)? + .into_iter() + .filter_map( + |FitsImage { + bitpix, + bscale, + bzero, + blank, + wcs, + raw_bytes, + header, + .. + }| { + if let Some(wcs) = wcs { + let image = Image::from_fits_hdu( + &gl, + wcs, + bitpix, + raw_bytes.as_ref(), + bscale, + bzero, + blank, + camera_coo_sys, + ) + .ok()?; + Some((image, header)) + } else { + None + } + }, + ) + .collect::<(Vec<_>, Vec<_>)>(); + + Ok((images, headers)) + }; let (images, headers) = match gz { fitsrs::gz::GzReader::GzReader(bytes) => parse_fits_images_from_bytes(bytes.get_ref())?, @@ -1272,7 +1273,6 @@ impl App { if images.is_empty() { Err(JsValue::from_str("no images have been parsed")) } else { - let layer = ImageLayer { images, id: layer.clone(), @@ -1290,11 +1290,14 @@ impl App { )?; self.request_redraw = true; - let obj: js_sys::Object = serde_wasm_bindgen::to_value(¶ms)? - .dyn_into()?; + let obj: js_sys::Object = serde_wasm_bindgen::to_value(¶ms)?.dyn_into()?; use std::iter::FromIterator; - let arr = js_sys::Array::from_iter(headers.iter().map(|header| serde_wasm_bindgen::to_value(&header).unwrap())); + let arr = js_sys::Array::from_iter( + headers + .iter() + .map(|header| serde_wasm_bindgen::to_value(&header).unwrap()), + ); js_sys::Reflect::set(&obj, &"headers".into(), &arr).unwrap(); let promise = js_sys::Promise::resolve(&obj.into()); diff --git a/src/core/src/shaders.rs b/src/core/src/shaders.rs index f305194d..3e75f00c 100644 --- a/src/core/src/shaders.rs +++ b/src/core/src/shaders.rs @@ -3,28 +3,925 @@ use std::collections::HashMap; pub fn get_all() -> HashMap<&'static str, &'static str> { let mut out = HashMap::new(); out.insert( - r"line_inst_ndc.vert", + r"hips_rasterizer_rgba.frag", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 p_a; -layout (location = 1) in vec2 p_b; -layout (location = 2) in vec2 vertex; +precision highp sampler2DArray; -out vec2 l; +uniform sampler2DArray tex; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} void main() { - vec2 x_b = p_b - p_a; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + vec4 color_start = uvw2c_rgba(frag_uv_start); + vec4 color_end = uvw2c_rgba(frag_uv_end); - float ndc2pix = 2.0 / u_width; + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = opacity * out_frag_color.a; +}"#, + ); + out.insert( + r"hips_rasterizer_raster.vert", + r#"#version 300 es +precision highp float; - vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - gl_Position = vec4(p, 0.f, 1.f); - l = vec2(0.0, vertex.y); +layout (location = 0) in vec3 xyz; +layout (location = 1) in vec3 uv_start; +layout (location = 2) in vec3 uv_end; +layout (location = 3) in float time_tile_received; + +out vec3 frag_uv_start; +out vec3 frag_uv_end; +out float frag_blending_factor; + +uniform mat3 inv_model; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float current_time; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_w = inv_model * xyz; + vec2 p_clip = proj(p_w); + + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); + + frag_uv_start = uv_start; + frag_uv_end = uv_end; + frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); +}"#, + ); + out.insert( + r"image_base.vert", + r#"#version 300 es +precision highp float; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"line_base.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 ndc_pos; + +out float l; + +void main() { + gl_Position = vec4( + ndc_pos, + 0.0, + 1.0 + ); +}"#, + ); + out.insert( + r"catalogs_ortho.frag", + r#"#version 300 es +precision highp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + if (out_p.z < 0.f) { + discard; + } + + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"hips3d_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_f32(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"fits_i32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; + +out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +void main() { + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; + + out_frag_color = uv2c_i32(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -299,659 +1196,39 @@ void main() { }"#, ); out.insert( - r"catalogs_aitoff.vert", + r"line_base.frag", r#"#version 300 es + precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; -uniform float current_time; -uniform mat3 inv_model; +out vec4 color; +in vec2 l; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; +uniform vec4 u_color; +uniform float u_thickness; +uniform float u_width; +uniform float u_height; -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; +void main() { + if (l.x > 0.05) { + discard; } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; + color = u_color; + + float dist = abs((u_thickness + 2.0) * l.y); + + float half_thickness = (u_thickness + 2.0) * 0.5; + color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_aitoff(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; }"#, ); out.insert( - r"hips_raytracer_raytracer.vert", + r"moc_base.vert", r#"#version 300 es precision highp float; -precision highp int; - -layout (location = 0) in vec2 pos_clip_space; -layout (location = 1) in vec3 pos_world_space; - -out vec2 out_clip_pos; -out vec3 frag_pos; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform mat3 model; - -void main() { - vec2 uv = pos_clip_space * 0.5 + 0.5; - - frag_pos = model * pos_world_space; - - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); - out_clip_pos = pos_clip_space; -}"#, - ); - out.insert( - r"hips_rasterizer_rgba.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec4 color_start = uvw2c_rgba(frag_uv_start); - vec4 color_end = uvw2c_rgba(frag_uv_end); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"image_sampler.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -void main() { - out_frag_color = texture(tex, frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_u8.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_u8(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips3d_raster.vert", - r#"#version 300 es -precision highp float; - layout (location = 0) in vec2 lonlat; -layout (location = 1) in vec3 uv; -out vec3 frag_uv; - -uniform mat3 inv_model; +uniform mat3 u_2world; uniform vec2 ndc_to_clip; uniform float czf; @@ -1058,13 +1335,11 @@ vec2 proj(vec3 p) { void main() { vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = inv_model * p_xyz; - vec2 p_clip = proj(p_w.xyz); + vec3 p_w = u_2world * p_xyz; + vec2 p_clip = proj(p_w); vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv = uv; + gl_Position = vec4(p_ndc, 0.f, 1.f); }"#, ); out.insert( @@ -1601,416 +1876,6 @@ void main() { out_frag_color = mix(color_start, color_end, frag_blending_factor); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_i32.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_i32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_f32.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2D; -precision highp int; - -out vec4 out_frag_color; -in vec2 frag_uv; - -uniform sampler2D tex; -uniform float opacity; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uv2c_f32(vec2 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uv2c_i32(vec2 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return val2c(val); -} - -vec4 uv2c_i16(vec2 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return val2c(val); -} - -vec4 uv2c_u8(vec2 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -void main() { - vec2 uv = frag_uv; - uv.y = 1.0 - uv.y; - - out_frag_color = uv2c_f32(frag_uv); - out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -2027,12 +1892,405 @@ void main() { }"#, ); out.insert( - r"hips3d_i32.frag", + r"hips_raytracer_i16.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2DArray; +precision highp int; + +uniform sampler2DArray tex; + +in vec3 frag_pos; +in vec2 out_clip_pos; +out vec4 out_frag_color; + +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} + +void main() { + vec3 uv = xyz2uv(normalize(frag_pos)); + + uv.y = 1.0 - uv.y; + vec4 c = uvw2c_i16(uv); + + + out_frag_color = c; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips3d_u8.frag", r#"#version 300 es precision highp float; precision highp sampler3D; -precision highp isampler3D; -precision highp usampler3D; uniform sampler3D tex; @@ -2288,41 +2546,167 @@ void main() { vec3 uv = vec3(frag_uv.xyz); uv.y = 1.0 - uv.y; - vec4 color = uvw2c_i32(uv); + vec4 color = uvw2c_u8(uv); out_frag_color = color; out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"image_base.vert", + r"catalogs_tan.vert", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 ndc_pos; +layout (location = 0) in vec2 offset; layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} -out vec2 frag_uv; void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_gnomonic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; }"#, ); out.insert( - r"hips3d_i16.frag", + r"hips_raytracer_rgba.frag", r#"#version 300 es precision highp float; -precision highp sampler3D; -precision highp isampler3D; -precision highp usampler3D; +precision highp sampler2DArray; +precision highp int; -uniform sampler3D tex; - -in vec3 frag_uv; +uniform sampler2DArray tex; +in vec2 out_clip_pos; +in vec3 frag_pos; out vec4 out_frag_color; +struct Tile { + int uniq; // Healpix cell + int texture_idx; // Index in the texture buffer + float start_time; // Absolute time that the load has been done in ms + float empty; +}; + +uniform Tile textures_tiles[12]; + uniform float scale; uniform float offset; uniform float blank; @@ -2564,44 +2948,339 @@ vec4 uvw2c_u8(vec3 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } +const float TWICE_PI = 6.28318530718; +const float PI = 3.141592653589793; +const float FOUR_OVER_PI = 1.27323954474; +const float TRANSITION_Z = 0.66666666666; +const float TRANSITION_Z_INV = 1.5; + +int quarter(vec2 p) { + int x_neg = int(p.x < 0.0); + int y_neg = int(p.y < 0.0); + int q = (x_neg + y_neg) | (y_neg << 1); + return q; +} + +float xpm1(vec2 p) { + bool x_neg = (p.x < 0.0); + bool y_neg = (p.y < 0.0); + float lon = atan(abs(p.y), abs(p.x)); + float x02 = lon * FOUR_OVER_PI; + if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 + return 1.0 - x02; + } else { + return x02 - 1.0; + } +} + +float one_minus_z_pos(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + + if (d2 < 1e-1) { // <=> dec > 84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return 1.0f - p.z; +} + +float one_minus_z_neg(vec3 p) { + float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 + if (d2 < 1e-1f) { // <=> dec < -84.27 deg + return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); + } + return p.z + 1.0; +} + +int ij2z(int i, int j) { + int i4 = i | (j << 2); + + int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; + int i5 = i4 ^ j4 ^ (j4 << 1); + + return i5; +} + +struct HashDxDy { + int idx; + float dx; + float dy; +}; + +uniform sampler2D ang2pixd; +HashDxDy hash_with_dxdy2(vec2 radec) { + vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); + vec3 v = texture(ang2pixd, aa).rgb; + return HashDxDy( + int(v.x * 255.0), + v.y, + v.z + ); +} +HashDxDy hash_with_dxdy(int depth, vec3 p) { + + int nside = 1 << depth; + float half_nside = float(nside) * 0.5; + + float x_pm1 = xpm1(p.xy); + int q = quarter(p.xy); + + int d0h = 0; + vec2 p_proj = vec2(0.0); + if (p.z > TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); + d0h = q; + } else if (p.z < -TRANSITION_Z) { + float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); + p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); + d0h = q + 8; + } else { + float y_pm1 = p.z * TRANSITION_Z_INV; + int q01 = int(x_pm1 > y_pm1); // 0/1 + int q12 = int(x_pm1 >= -y_pm1); // 0\1 + int q03 = 1 - q12; // 1\0 + int q1 = q01 & q12; // = 1 if q1, 0 else + p_proj = vec2( + x_pm1 - float(q01 + q12 - 1), + y_pm1 + float(q01 + q03) + ); + d0h = ((q01 + q03) << 2) + ((q + q1) & 3); + } + + float x = (half_nside * (p_proj.x + p_proj.y)); + float y = (half_nside * (p_proj.y - p_proj.x)); + int i = int(x); + int j = int(y); + + return HashDxDy( + (d0h << (depth << 1)) + ij2z(i, j), + x - float(i), + y - float(j) + ); +} +vec3 xyz2uv(vec3 xyz) { + HashDxDy result = hash_with_dxdy(0, xyz.zxy); + + int idx = result.idx; + vec2 offset = vec2(result.dy, result.dx); + Tile tile = textures_tiles[idx]; + + return vec3(offset, float(tile.texture_idx)); +} uniform float opacity; +uniform vec4 no_tile_color; void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; + vec3 uv = xyz2uv(normalize(frag_pos)); + vec4 c = uvw2c_rgba(uv); - vec4 color = uvw2c_i16(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; + out_frag_color = c; + out_frag_color = vec4(c.rgb, opacity * c.a); }"#, ); out.insert( - r"line_base.frag", + r"fits_u8.frag", r#"#version 300 es - precision highp float; +precision highp sampler2D; +precision highp int; -out vec4 color; -in vec2 l; +out vec4 out_frag_color; +in vec2 frag_uv; -uniform vec4 u_color; -uniform float u_thickness; -uniform float u_width; -uniform float u_height; +uniform sampler2D tex; +uniform float opacity; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uv2c_f32(vec2 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uv2c_i32(vec2 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return val2c(val); +} + +vec4 uv2c_i16(vec2 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return val2c(val); +} + +vec4 uv2c_u8(vec2 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} void main() { - if (l.x > 0.05) { - discard; - } else { - color = u_color; + vec2 uv = frag_uv; + uv.y = 1.0 - uv.y; - float dist = abs((u_thickness + 2.0) * l.y); - - float half_thickness = (u_thickness + 2.0) * 0.5; - color.a = color.a * (1.0 - smoothstep(half_thickness - 1.0, half_thickness, dist)); - } + out_frag_color = uv2c_u8(frag_uv); + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( @@ -3001,159 +3680,282 @@ void main() { }"#, ); out.insert( - r"line_inst_lonlat.vert", + r"hips3d_i32.frag", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 p_a_lonlat; -layout (location = 1) in vec2 p_b_lonlat; -layout (location = 2) in vec2 vertex; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float u_width; -uniform float u_height; -uniform float u_thickness; +uniform sampler3D tex; -out vec2 l; +in vec3 frag_uv; -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; +out vec4 out_frag_color; -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); } -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); } -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); + return pow2_f(x, min_value, max_value); } } +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + void main() { - vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); - vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); - vec3 p_a_w = u_2world * p_a_xyz; - vec3 p_b_w = u_2world * p_b_xyz; - vec2 p_a_clip = proj(p_a_w); - vec2 p_b_clip = proj(p_b_w); + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; - vec2 da = p_a_clip - p_b_clip; + vec4 color = uvw2c_i32(uv); - vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); - vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); - - vec2 x_b = p_b_ndc - p_a_ndc; - vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); - - float ndc2pix = 2.0 / u_width; - - vec2 p_ndc_x = x_b * vertex.x; - vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; - - vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; - gl_Position = vec4(p_ndc, 0.f, 1.f); - - l = vec2(dot(da, da), vertex.y); + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"hips_raytracer_i16.frag", + r"hips_raytracer_rgba2cmap.frag", r#"#version 300 es precision highp float; precision highp sampler2DArray; +precision highp sampler2DArray; +precision highp isampler2DArray; precision highp int; -uniform sampler2DArray tex; - in vec3 frag_pos; in vec2 out_clip_pos; out vec4 out_frag_color; @@ -3168,6 +3970,7 @@ struct Tile { uniform Tile textures_tiles[12]; uniform float opacity; +uniform sampler2DArray tex; uniform float scale; uniform float offset; @@ -3531,45 +4334,10 @@ vec3 xyz2uv(vec3 xyz) { void main() { vec3 uv = xyz2uv(normalize(frag_pos)); - - uv.y = 1.0 - uv.y; - vec4 c = uvw2c_i16(uv); - + vec4 c = uvw2cmap_rgba(uv); out_frag_color = c; out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"fits_base.vert", - r#"#version 300 es -precision highp float; -precision highp int; - -layout (location = 0) in vec2 ndc_pos; -layout (location = 1) in vec2 uv; - -out vec2 frag_uv; - -void main() { - gl_Position = vec4(ndc_pos, 0.0, 1.0); - frag_uv = uv; -}"#, - ); - out.insert( - r"line_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 ndc_pos; - -out float l; - -void main() { - gl_Position = vec4( - ndc_pos, - 0.0, - 1.0 - ); }"#, ); out.insert( @@ -3973,2284 +4741,179 @@ void main() { }"#, ); out.insert( - r"hips_raytracer_backcolor.frag", + r"image_sampler.frag", r#"#version 300 es precision highp float; -precision highp int; +precision highp sampler2D; out vec4 out_frag_color; +in vec2 frag_uv; -uniform vec3 color; - -void main() { - out_frag_color = vec4(color, 1.0); -}"#, - ); - out.insert( - r"hips_raytracer_rgba.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2DArray; -precision highp int; - -uniform sampler2DArray tex; - -in vec2 out_clip_pos; -in vec3 frag_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - +uniform sampler2D tex; uniform float opacity; -uniform vec4 no_tile_color; void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2c_rgba(uv); - - out_frag_color = c; - out_frag_color = vec4(c.rgb, opacity * c.a); + out_frag_color = texture(tex, frag_uv); + out_frag_color.a = out_frag_color.a * opacity; }"#, ); out.insert( - r"catalogs_catalog.frag", + r"catalogs_healpix.vert", r#"#version 300 es precision highp float; -in vec2 out_uv; -in vec3 out_p; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_healpix(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"passes_post_fragment_100es.frag", + r#"#version 300 es +precision highp float; + +in vec2 v_tc; out vec4 color; -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"moc_base.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 lonlat; +uniform sampler2D fbo_tex; -uniform mat3 u_2world; -uniform vec2 ndc_to_clip; -uniform float czf; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); +vec3 srgb_from_linear(vec3 rgb) { + bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); + vec3 lower = rgb * vec3(3294.6); + vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); + return mix(higher, lower, vec3(cutoff)); } -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } +vec4 srgba_from_linear(vec4 rgba) { + return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); } void main() { - vec3 p_xyz = lonlat2xyz(lonlat); - vec3 p_w = u_2world * p_xyz; - vec2 p_clip = proj(p_w); + color = texture(fbo_tex, v_tc); - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.f, 1.f); -}"#, - ); - out.insert( - r"catalogs_tan.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_gnomonic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_mercator.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mercator(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_rasterizer_u8.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2DArray; - -uniform sampler2DArray tex; - -in vec3 frag_uv_start; -in vec3 frag_uv_end; -in float frag_blending_factor; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv0 = frag_uv_start; - vec3 uv1 = frag_uv_end; - uv0.y = 1.0 - uv0.y; - uv1.y = 1.0 - uv1.y; - - vec4 color_start = uvw2c_u8(uv0); - vec4 color_end = uvw2c_u8(uv1); - - out_frag_color = mix(color_start, color_end, frag_blending_factor); - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_ortho.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_orthographic(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_mollweide.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_mollweide(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"catalogs_arc.vert", - r#"#version 300 es -precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; - -uniform float current_time; -uniform mat3 inv_model; - -uniform vec2 ndc_to_clip; -uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p = inv_model * center; - - vec2 center_pos_clip_space = world2clip_arc(p); - - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; -}"#, - ); - out.insert( - r"hips_rasterizer_raster.vert", - r#"#version 300 es -precision highp float; - -layout (location = 0) in vec3 xyz; -layout (location = 1) in vec3 uv_start; -layout (location = 2) in vec3 uv_end; -layout (location = 3) in float time_tile_received; - -out vec3 frag_uv_start; -out vec3 frag_uv_end; -out float frag_blending_factor; - -uniform mat3 inv_model; -uniform vec2 ndc_to_clip; -uniform float czf; -uniform float current_time; - -const float PI = 3.141592653589793; -const float SQRT_2 = 1.41421356237309504880168872420969808; - -vec2 w2c_sin(vec3 p) { - vec2 q = vec2(-p.x, p.y); - return p.z >= 0.f ? q : normalize(q); -} - -vec2 w2c_sin_no_backface(vec3 p) { - return vec2(-p.x, p.y); -} - -vec2 w2c_ait(vec3 p) { - float r = length(p.zx); - float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) - w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma - float y2d = p.y / w; - - float x2d = 0.0; - if (abs(p.x) < 5e-3) { - float x_over_r = p.x/r; - x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; - } else { - w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) - x2d = sign(-p.x) * w; - } - - return vec2(x2d * 0.5, y2d) / SQRT_2; -} -const float eps = 1.25e-8; -const int n_iter = 100; - -float newton_solve(float z) { - float cte = PI * z; - float x = 2.0 * asin(z); - float f = x + sin(x) - cte; - int i = 0; - while (abs(f) > eps && i < n_iter) { - x -= f / (1.0 + cos(x)); - f = x + sin(x) - cte; - i += 1; - } - - return 0.5 * x; -} - -vec2 w2c_mol(vec3 p) { - float g = newton_solve(p.y); - - float sg = sin(g); - float cg = cos(g); - return vec2((atan(-p.x, p.z) * cg) / PI, sg); -} -vec2 w2c_tan(vec3 p) { - p.z = max(p.z, 1e-2); - return vec2(-p.x, p.y) / (p.z*PI); -} -vec2 w2c_stg(vec3 p) { - float w = (1.0 + p.z) * 0.5; - return vec2(-p.x, p.y) / (PI * w); -} -vec2 w2c_zea(vec3 p) { - float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] - return vec2(-p.x, p.y) * 0.5 / w; -} -vec2 w2c_mer(vec3 p) { - return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; -} - -vec3 lonlat2xyz(vec2 lonlat) { - float t = lonlat.x; - float tc = cos(t); - float ts = sin(t); - - float d = lonlat.y; - float dc = cos(d); - float ds = sin(d); - - return vec3(dc * ts, ds, dc * tc); -} - -uniform int u_proj; - -vec2 proj(vec3 p) { - if (u_proj == 0) { - return w2c_tan(p); - } else if (u_proj == 1) { - return w2c_stg(p); - } else if (u_proj == 2) { - return w2c_sin(p); - } else if (u_proj == 3) { - return w2c_zea(p); - } else if (u_proj == 4) { - return w2c_ait(p); - } else if (u_proj == 5) { - return w2c_mol(p); - } else { - return w2c_mer(p); - } -} - -void main() { - vec3 p_w = inv_model * xyz; - vec2 p_clip = proj(p_w); - - vec2 p_ndc = p_clip / (ndc_to_clip * czf); - gl_Position = vec4(p_ndc, 0.0, 1.0); - - frag_uv_start = uv_start; - frag_uv_end = uv_end; - frag_blending_factor = min((current_time - time_tile_received) / 200.0, 1.0); -}"#, - ); - out.insert( - r"hips3d_u8.frag", - r#"#version 300 es -precision highp float; -precision highp sampler3D; - -uniform sampler3D tex; - -in vec3 frag_uv; - -out vec4 out_frag_color; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} - -uniform float opacity; - -void main() { - vec3 uv = vec3(frag_uv.xyz); - uv.y = 1.0 - uv.y; - - vec4 color = uvw2c_u8(uv); - - out_frag_color = color; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"hips_raytracer_rgba2cmap.frag", - r#"#version 300 es -precision highp float; -precision highp sampler2DArray; -precision highp sampler2DArray; -precision highp isampler2DArray; -precision highp int; - -in vec3 frag_pos; -in vec2 out_clip_pos; -out vec4 out_frag_color; - -struct Tile { - int uniq; // Healpix cell - int texture_idx; // Index in the texture buffer - float start_time; // Absolute time that the load has been done in ms - float empty; -}; - -uniform Tile textures_tiles[12]; - -uniform float opacity; -uniform sampler2DArray tex; - -uniform float scale; -uniform float offset; -uniform float blank; -uniform float min_value; -uniform float max_value; -uniform int H; -uniform float reversed; - -uniform sampler2D colormaps; -uniform float num_colormaps; -uniform float colormap_id; - -vec4 colormap_f(float x) { - float id = (colormap_id + 0.5) / num_colormaps; - return texture(colormaps, vec2(x, id)); -} -float linear_f(float x, float min_value, float max_value) { - return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); -} - -float sqrt_f(float x, float min_value, float max_value) { - float a = linear_f(x, min_value, max_value); - return sqrt(a); -} - -float log_f(float x, float min_value, float max_value) { - float y = linear_f(x, min_value, max_value); - float a = 1000.0; - return log(a*y + 1.0)/log(a); -} - -float asinh_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return asinh(10.0*d)/3.0; -} - -float pow2_f(float x, float min_value, float max_value) { - float d = linear_f(x, min_value, max_value); - return d*d; -} - -float transfer_func(int H, float x, float min_value, float max_value) { - if (H == 0) { - return linear_f(x, min_value, max_value); - } else if (H == 1) { - return sqrt_f(x, min_value, max_value); - } else if (H == 2) { - return log_f(x, min_value, max_value); - } else if (H == 3) { - return asinh_f(x, min_value, max_value); - } else { - return pow2_f(x, min_value, max_value); - } -} - -uniform float k_gamma; -uniform float k_saturation; -uniform float k_contrast; -uniform float k_brightness; -uniform float k_exposure; - -vec4 apply_gamma(vec4 ic, float g) { - float new_r = pow(ic.r, g); - float new_g = pow(ic.g, g); - float new_b = pow(ic.b, g); - - return vec4(new_r, new_g, new_b, ic.a); -} - -vec4 apply_saturation(vec4 color, float value) { - const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); - vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); - - return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); -} - -vec4 apply_contrast(vec4 color, float value) { - return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); -} - -vec4 apply_brightness(vec4 color, float value) { - return vec4(color.rgb + value, color.a); -} - -vec4 apply_exposure(vec4 color, float value) { - return vec4((1.0 + value) * color.rgb, color.a); -} - -vec4 apply_tonal(vec4 color) { - return apply_gamma( - apply_saturation( - apply_contrast( - apply_brightness(color, k_brightness), - k_contrast - ), - k_saturation - ), - k_gamma - ); -} -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} -highp float decode_f32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - if (abs(Exponent + 127.0) < 1e-3) { - return 0.0; - } - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; -} - -int decode_i32(vec4 rgba) { - int r = int(rgba.r * 255.0 + 0.5); - int g = int(rgba.g * 255.0 + 0.5); - int b = int(rgba.b * 255.0 + 0.5); - int a = int(rgba.a * 255.0 + 0.5); - - int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer - - return value; -} - -int decode_i16(vec2 rg) { - int r = int(rg.r * 255.0 + 0.5); - int g = int(rg.g * 255.0 + 0.5); - - int value = (r << 8) | g; // Combine into a 16-bit integer - - if (value >= 32768) { - value -= 65536; - } - - return value; -} - -uint decode_u8(float r) { - uint value = uint(r * 255.0 + 0.5); - return value; -} - - - - -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 val2c_f32(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); - return apply_tonal(new_color); -} - -vec4 val2c(float x) { - float alpha = x * scale + offset; - alpha = transfer_func(H, alpha, min_value, max_value); - - alpha = mix(alpha, 1.0 - alpha, reversed); - - vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); - return apply_tonal(new_color); -} - -vec4 uvw2c_f32(vec3 uv) { - float val = decode_f32(texture(tex, uv).rgba*255.0); - return val2c_f32(val); -} - -vec4 uvw2c_i32(vec3 uv) { - float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_i16(vec3 uv) { - float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); -} - -vec4 uvw2c_u8(vec3 uv) { - float val = float(decode_u8(texture(tex, uv).r)); - return val2c(val); -} -const float TWICE_PI = 6.28318530718; -const float PI = 3.141592653589793; -const float FOUR_OVER_PI = 1.27323954474; -const float TRANSITION_Z = 0.66666666666; -const float TRANSITION_Z_INV = 1.5; - -int quarter(vec2 p) { - int x_neg = int(p.x < 0.0); - int y_neg = int(p.y < 0.0); - int q = (x_neg + y_neg) | (y_neg << 1); - return q; -} - -float xpm1(vec2 p) { - bool x_neg = (p.x < 0.0); - bool y_neg = (p.y < 0.0); - float lon = atan(abs(p.y), abs(p.x)); - float x02 = lon * FOUR_OVER_PI; - if (x_neg != y_neg) { // Could be replaced by a sign copy from (x_neg ^ y_neg) << 32 - return 1.0 - x02; - } else { - return x02 - 1.0; - } -} - -float one_minus_z_pos(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - - if (d2 < 1e-1) { // <=> dec > 84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return 1.0f - p.z; -} - -float one_minus_z_neg(vec3 p) { - float d2 = dot(p.xy, p.xy); // z = sqrt(1 - d2) AND sqrt(1 - x) = 1 - x / 2 - x^2 / 8 - x^3 / 16 - 5 x^4/128 - 7 * x^5/256 - if (d2 < 1e-1f) { // <=> dec < -84.27 deg - return d2 * (0.5 + d2 * (0.125 + d2 * (0.0625 + d2 * (0.0390625 + d2 * 0.02734375)))); - } - return p.z + 1.0; -} - -int ij2z(int i, int j) { - int i4 = i | (j << 2); - - int j4 = (i4 ^ (i4 >> 1)) & 0x22222222; - int i5 = i4 ^ j4 ^ (j4 << 1); - - return i5; -} - -struct HashDxDy { - int idx; - float dx; - float dy; -}; - -uniform sampler2D ang2pixd; -HashDxDy hash_with_dxdy2(vec2 radec) { - vec2 aa = vec2(radec.x/TWICE_PI + 1.0, (radec.y/PI) + 0.5); - vec3 v = texture(ang2pixd, aa).rgb; - return HashDxDy( - int(v.x * 255.0), - v.y, - v.z - ); -} -HashDxDy hash_with_dxdy(int depth, vec3 p) { - - int nside = 1 << depth; - float half_nside = float(nside) * 0.5; - - float x_pm1 = xpm1(p.xy); - int q = quarter(p.xy); - - int d0h = 0; - vec2 p_proj = vec2(0.0); - if (p.z > TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_pos(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, 2.0 - sqrt_3_one_min_z); - d0h = q; - } else if (p.z < -TRANSITION_Z) { - float sqrt_3_one_min_z = sqrt(3.0 * one_minus_z_neg(p)); - p_proj = vec2(x_pm1 * sqrt_3_one_min_z, sqrt_3_one_min_z); - d0h = q + 8; - } else { - float y_pm1 = p.z * TRANSITION_Z_INV; - int q01 = int(x_pm1 > y_pm1); // 0/1 - int q12 = int(x_pm1 >= -y_pm1); // 0\1 - int q03 = 1 - q12; // 1\0 - int q1 = q01 & q12; // = 1 if q1, 0 else - p_proj = vec2( - x_pm1 - float(q01 + q12 - 1), - y_pm1 + float(q01 + q03) - ); - d0h = ((q01 + q03) << 2) + ((q + q1) & 3); - } - - float x = (half_nside * (p_proj.x + p_proj.y)); - float y = (half_nside * (p_proj.y - p_proj.x)); - int i = int(x); - int j = int(y); - - return HashDxDy( - (d0h << (depth << 1)) + ij2z(i, j), - x - float(i), - y - float(j) - ); -} -vec3 xyz2uv(vec3 xyz) { - HashDxDy result = hash_with_dxdy(0, xyz.zxy); - - int idx = result.idx; - vec2 offset = vec2(result.dy, result.dx); - Tile tile = textures_tiles[idx]; - - return vec3(offset, float(tile.texture_idx)); -} - -void main() { - vec3 uv = xyz2uv(normalize(frag_pos)); - vec4 c = uvw2cmap_rgba(uv); - - out_frag_color = c; - out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"moc_base.frag", - r#"#version 300 es - -precision highp float; -out vec4 color; - -uniform vec4 u_color; - -void main() { - color = u_color; }"#, ); out.insert( @@ -6459,23 +5122,18 @@ void main() { }"#, ); out.insert( - r"catalogs_healpix.vert", + r"hips3d_raster.vert", r#"#version 300 es precision highp float; -layout (location = 0) in vec2 offset; -layout (location = 1) in vec2 uv; -layout (location = 2) in vec3 center; +layout (location = 0) in vec2 lonlat; +layout (location = 1) in vec3 uv; + +out vec3 frag_uv; -uniform float current_time; uniform mat3 inv_model; - uniform vec2 ndc_to_clip; uniform float czf; -uniform vec2 kernel_size; - -out vec2 out_uv; -out vec3 out_p; const float PI = 3.141592653589793; const float SQRT_2 = 1.41421356237309504880168872420969808; @@ -6578,30 +5236,199 @@ vec2 proj(vec3 p) { } } - void main() { - vec3 p = inv_model * center; + vec3 p_xyz = lonlat2xyz(lonlat); + vec3 p_w = inv_model * p_xyz; + vec2 p_clip = proj(p_w.xyz); - vec2 center_pos_clip_space = world2clip_healpix(p); + vec2 p_ndc = p_clip / (ndc_to_clip * czf); + gl_Position = vec4(p_ndc, 0.0, 1.0); - vec2 pos_clip_space = center_pos_clip_space; - gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); - - out_uv = uv; - out_p = p; + frag_uv = uv; }"#, ); out.insert( - r"hips3d_f32.frag", + r"line_inst_lonlat.vert", r#"#version 300 es precision highp float; -precision highp sampler3D; +layout (location = 0) in vec2 p_a_lonlat; +layout (location = 1) in vec2 p_b_lonlat; +layout (location = 2) in vec2 vertex; -uniform sampler3D tex; +uniform mat3 u_2world; +uniform vec2 ndc_to_clip; +uniform float czf; +uniform float u_width; +uniform float u_height; +uniform float u_thickness; -in vec3 frag_uv; +out vec2 l; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p_a_xyz = lonlat2xyz(p_a_lonlat); + vec3 p_b_xyz = lonlat2xyz(p_b_lonlat); + vec3 p_a_w = u_2world * p_a_xyz; + vec3 p_b_w = u_2world * p_b_xyz; + vec2 p_a_clip = proj(p_a_w); + vec2 p_b_clip = proj(p_b_w); + + vec2 da = p_a_clip - p_b_clip; + + vec2 p_a_ndc = p_a_clip / (ndc_to_clip * czf); + vec2 p_b_ndc = p_b_clip / (ndc_to_clip * czf); + + vec2 x_b = p_b_ndc - p_a_ndc; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p_ndc_x = x_b * vertex.x; + vec2 p_ndc_y = (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + + vec2 p_ndc = p_a_ndc + p_ndc_x + p_ndc_y; + gl_Position = vec4(p_ndc, 0.f, 1.f); + + l = vec2(dot(da, da), vertex.y); +}"#, + ); + out.insert( + r"line_inst_ndc.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 p_a; +layout (location = 1) in vec2 p_b; +layout (location = 2) in vec2 vertex; + +out vec2 l; + +uniform float u_width; +uniform float u_height; +uniform float u_thickness; + +void main() { + vec2 x_b = p_b - p_a; + vec2 y_b = normalize(vec2(-x_b.y, x_b.x)); + + float ndc2pix = 2.0 / u_width; + + vec2 p = p_a + x_b * vertex.x + (u_thickness + 2.0) * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix; + gl_Position = vec4(p, 0.f, 1.f); + l = vec2(0.0, vertex.y); +}"#, + ); + out.insert( + r"fits_f32.frag", + r#"#version 300 es +precision highp float; +precision highp sampler2D; +precision highp int; out vec4 out_frag_color; +in vec2 frag_uv; + +uniform sampler2D tex; +uniform float opacity; uniform float scale; uniform float offset; @@ -6703,21 +5530,6 @@ vec4 apply_tonal(vec4 color) { k_gamma ); } -vec3 rgb2hsv(vec3 c) { - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} highp float decode_f32(highp vec4 rgba) { highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; @@ -6761,49 +5573,6 @@ uint decode_u8(float r) { -vec4 uvw2c_r(vec3 uv) { - vec2 va = texture(tex, uv).ra; - - va.x = transfer_func(H, va.x, min_value, max_value); - - va.x = mix(va.x, 1.0 - va.x, reversed); - - vec4 c = colormap_f(va.x); - return apply_tonal(c); -} - -vec4 uvw2c_rgba(vec3 uv) { - vec4 c = texture(tex, uv).rgba; - - c.r = transfer_func(H, c.r, min_value, max_value); - c.g = transfer_func(H, c.g, min_value, max_value); - c.b = transfer_func(H, c.b, min_value, max_value); - - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} - -vec4 uvw2c_ra(vec3 uv) { - vec2 c = texture(tex, uv).rg; - - c.r = transfer_func(H, c.r, min_value, max_value); - - c.r = mix(c.r, 1.0 - c.r, reversed); - - vec3 color = colormap_f(c.r).rgb; - - return apply_tonal(vec4(color, c.g)); -} - -vec4 uvw2cmap_rgba(vec3 uv) { - float v = texture(tex, uv).r; - v = transfer_func(H, v, min_value, max_value); - vec4 c = colormap_f(v); - c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); - - return apply_tonal(c); -} vec4 val2c_f32(float x) { float alpha = x * scale + offset; @@ -6825,74 +5594,32 @@ vec4 val2c(float x) { return apply_tonal(new_color); } -vec4 uvw2c_f32(vec3 uv) { +vec4 uv2c_f32(vec2 uv) { float val = decode_f32(texture(tex, uv).rgba*255.0); return val2c_f32(val); } -vec4 uvw2c_i32(vec3 uv) { +vec4 uv2c_i32(vec2 uv) { float val = float(decode_i32(texture(tex, uv).rgba)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); + return val2c(val); } -vec4 uvw2c_i16(vec3 uv) { +vec4 uv2c_i16(vec2 uv) { float val = float(decode_i16(texture(tex, uv).rg)); - return mix(val2c(val), vec4(0.0), float(val == -1.0)); + return val2c(val); } -vec4 uvw2c_u8(vec3 uv) { +vec4 uv2c_u8(vec2 uv) { float val = float(decode_u8(texture(tex, uv).r)); return val2c(val); } -uniform float opacity; - void main() { - vec3 uv = vec3(frag_uv.xyz); + vec2 uv = frag_uv; uv.y = 1.0 - uv.y; - vec4 color = uvw2c_f32(uv); - - out_frag_color = color; + out_frag_color = uv2c_f32(frag_uv); out_frag_color.a = out_frag_color.a * opacity; -}"#, - ); - out.insert( - r"catalogs_ortho.frag", - r#"#version 300 es -precision highp float; - -in vec2 out_uv; -in vec3 out_p; - -out vec4 color; - -uniform sampler2D kernel_texture; -uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth -uniform float fov; -uniform float strength; -void main() { - if (out_p.z < 0.f) { - discard; - } - - color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); - color.r *= strength; -}"#, - ); - out.insert( - r"hips_raytracer_backcolor.vert", - r#"#version 300 es -precision highp float; -precision highp int; - -layout (location = 0) in vec2 pos_clip_space; - -uniform vec2 ndc_to_clip; -uniform float czf; - -void main() { - gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); }"#, ); out.insert( @@ -7167,19 +5894,288 @@ void main() { }"#, ); out.insert( - r"colormaps_colormap.vert", + r"hips_rasterizer_u8.frag", r#"#version 300 es precision highp float; -precision highp sampler2D; +precision highp sampler2DArray; -layout (location = 0) in vec2 position; -layout (location = 1) in vec2 uv; +uniform sampler2DArray tex; -out vec2 out_uv; +in vec3 frag_uv_start; +in vec3 frag_uv_end; +in float frag_blending_factor; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; void main() { - gl_Position = vec4(position, 0.f, 1.f); - out_uv = uv; + vec3 uv0 = frag_uv_start; + vec3 uv1 = frag_uv_end; + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + + vec4 color_start = uvw2c_u8(uv0); + vec4 color_end = uvw2c_u8(uv1); + + out_frag_color = mix(color_start, color_end, frag_blending_factor); + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.frag", + r#"#version 300 es +precision highp float; +precision highp int; + +out vec4 out_frag_color; + +uniform vec3 color; + +void main() { + out_frag_color = vec4(color, 1.0); }"#, ); out.insert( @@ -7577,6 +6573,150 @@ void main() { out_frag_color = c; out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_aitoff.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_aitoff(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"moc_base.frag", + r#"#version 300 es + +precision highp float; +out vec4 color; + +uniform vec4 u_color; + +void main() { + color = u_color; }"#, ); out.insert( @@ -7607,6 +6747,892 @@ void main() { color = colormap_f(opacity); color.a = o * alpha; +}"#, + ); + out.insert( + r"hips_raytracer_raytracer.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 pos_clip_space; +layout (location = 1) in vec3 pos_world_space; + +out vec2 out_clip_pos; +out vec3 frag_pos; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform mat3 model; + +void main() { + vec2 uv = pos_clip_space * 0.5 + 0.5; + + frag_pos = model * pos_world_space; + + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); + out_clip_pos = pos_clip_space; +}"#, + ); + out.insert( + r"catalogs_ortho.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_orthographic(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_mollweide.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mollweide(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"catalogs_catalog.frag", + r#"#version 300 es +precision highp float; + +in vec2 out_uv; +in vec3 out_p; + +out vec4 color; + +uniform sampler2D kernel_texture; +uniform float max_density; // max number of sources in a kernel sized HEALPix cell at the current depth +uniform float fov; +uniform float strength; +void main() { + color = texture(kernel_texture, out_uv) / max(log2(fov*100.0), 1.0); + color.r *= strength; +}"#, + ); + out.insert( + r"hips_raytracer_backcolor.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 pos_clip_space; + +uniform vec2 ndc_to_clip; +uniform float czf; + +void main() { + gl_Position = vec4(pos_clip_space / (ndc_to_clip * czf), 0.0, 1.0); +}"#, + ); + out.insert( + r"fits_base.vert", + r#"#version 300 es +precision highp float; +precision highp int; + +layout (location = 0) in vec2 ndc_pos; +layout (location = 1) in vec2 uv; + +out vec2 frag_uv; + +void main() { + gl_Position = vec4(ndc_pos, 0.0, 1.0); + frag_uv = uv; +}"#, + ); + out.insert( + r"catalogs_mercator.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_mercator(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"hips3d_i16.frag", + r#"#version 300 es +precision highp float; +precision highp sampler3D; +precision highp isampler3D; +precision highp usampler3D; + +uniform sampler3D tex; + +in vec3 frag_uv; + +out vec4 out_frag_color; + +uniform float scale; +uniform float offset; +uniform float blank; +uniform float min_value; +uniform float max_value; +uniform int H; +uniform float reversed; + +uniform sampler2D colormaps; +uniform float num_colormaps; +uniform float colormap_id; + +vec4 colormap_f(float x) { + float id = (colormap_id + 0.5) / num_colormaps; + return texture(colormaps, vec2(x, id)); +} +float linear_f(float x, float min_value, float max_value) { + return clamp((x - min_value)/(max_value - min_value), 0.0, 1.0); +} + +float sqrt_f(float x, float min_value, float max_value) { + float a = linear_f(x, min_value, max_value); + return sqrt(a); +} + +float log_f(float x, float min_value, float max_value) { + float y = linear_f(x, min_value, max_value); + float a = 1000.0; + return log(a*y + 1.0)/log(a); +} + +float asinh_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return asinh(10.0*d)/3.0; +} + +float pow2_f(float x, float min_value, float max_value) { + float d = linear_f(x, min_value, max_value); + return d*d; +} + +float transfer_func(int H, float x, float min_value, float max_value) { + if (H == 0) { + return linear_f(x, min_value, max_value); + } else if (H == 1) { + return sqrt_f(x, min_value, max_value); + } else if (H == 2) { + return log_f(x, min_value, max_value); + } else if (H == 3) { + return asinh_f(x, min_value, max_value); + } else { + return pow2_f(x, min_value, max_value); + } +} + +uniform float k_gamma; +uniform float k_saturation; +uniform float k_contrast; +uniform float k_brightness; +uniform float k_exposure; + +vec4 apply_gamma(vec4 ic, float g) { + float new_r = pow(ic.r, g); + float new_g = pow(ic.g, g); + float new_b = pow(ic.b, g); + + return vec4(new_r, new_g, new_b, ic.a); +} + +vec4 apply_saturation(vec4 color, float value) { + const vec3 luminosity_factor = vec3(0.2126, 0.7152, 0.0722); + vec3 grayscale = vec3(dot(color.rgb, luminosity_factor)); + + return vec4(mix(grayscale, color.rgb, 1.0 + value), color.a); +} + +vec4 apply_contrast(vec4 color, float value) { + return vec4(0.5 + (1.0 + value) * (color.rgb - 0.5), color.a); +} + +vec4 apply_brightness(vec4 color, float value) { + return vec4(color.rgb + value, color.a); +} + +vec4 apply_exposure(vec4 color, float value) { + return vec4((1.0 + value) * color.rgb, color.a); +} + +vec4 apply_tonal(vec4 color) { + return apply_gamma( + apply_saturation( + apply_contrast( + apply_brightness(color, k_brightness), + k_contrast + ), + k_saturation + ), + k_gamma + ); +} +vec3 rgb2hsv(vec3 c) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} +highp float decode_f32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + if (abs(Exponent + 127.0) < 1e-3) { + return 0.0; + } + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; +} + +int decode_i32(vec4 rgba) { + int r = int(rgba.r * 255.0 + 0.5); + int g = int(rgba.g * 255.0 + 0.5); + int b = int(rgba.b * 255.0 + 0.5); + int a = int(rgba.a * 255.0 + 0.5); + + int value = (r << 24) | (g << 16) | (b << 8) | a; // Combine into a 16-bit integer + + return value; +} + +int decode_i16(vec2 rg) { + int r = int(rg.r * 255.0 + 0.5); + int g = int(rg.g * 255.0 + 0.5); + + int value = (r << 8) | g; // Combine into a 16-bit integer + + if (value >= 32768) { + value -= 65536; + } + + return value; +} + +uint decode_u8(float r) { + uint value = uint(r * 255.0 + 0.5); + return value; +} + + + + +vec4 uvw2c_r(vec3 uv) { + vec2 va = texture(tex, uv).ra; + + va.x = transfer_func(H, va.x, min_value, max_value); + + va.x = mix(va.x, 1.0 - va.x, reversed); + + vec4 c = colormap_f(va.x); + return apply_tonal(c); +} + +vec4 uvw2c_rgba(vec3 uv) { + vec4 c = texture(tex, uv).rgba; + + c.r = transfer_func(H, c.r, min_value, max_value); + c.g = transfer_func(H, c.g, min_value, max_value); + c.b = transfer_func(H, c.b, min_value, max_value); + + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 uvw2c_ra(vec3 uv) { + vec2 c = texture(tex, uv).rg; + + c.r = transfer_func(H, c.r, min_value, max_value); + + c.r = mix(c.r, 1.0 - c.r, reversed); + + vec3 color = colormap_f(c.r).rgb; + + return apply_tonal(vec4(color, c.g)); +} + +vec4 uvw2cmap_rgba(vec3 uv) { + float v = texture(tex, uv).r; + v = transfer_func(H, v, min_value, max_value); + vec4 c = colormap_f(v); + c.rgb = mix(c.rgb, 1.0 - c.rgb, reversed); + + return apply_tonal(c); +} + +vec4 val2c_f32(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(isinf(x) || isnan(x))); + return apply_tonal(new_color); +} + +vec4 val2c(float x) { + float alpha = x * scale + offset; + alpha = transfer_func(H, alpha, min_value, max_value); + + alpha = mix(alpha, 1.0 - alpha, reversed); + + vec4 new_color = mix(colormap_f(alpha), vec4(0.0), float(x == blank)); + return apply_tonal(new_color); +} + +vec4 uvw2c_f32(vec3 uv) { + float val = decode_f32(texture(tex, uv).rgba*255.0); + return val2c_f32(val); +} + +vec4 uvw2c_i32(vec3 uv) { + float val = float(decode_i32(texture(tex, uv).rgba)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_i16(vec3 uv) { + float val = float(decode_i16(texture(tex, uv).rg)); + return mix(val2c(val), vec4(0.0), float(val == -1.0)); +} + +vec4 uvw2c_u8(vec3 uv) { + float val = float(decode_u8(texture(tex, uv).r)); + return val2c(val); +} + +uniform float opacity; + +void main() { + vec3 uv = vec3(frag_uv.xyz); + uv.y = 1.0 - uv.y; + + vec4 color = uvw2c_i16(uv); + + out_frag_color = color; + out_frag_color.a = out_frag_color.a * opacity; +}"#, + ); + out.insert( + r"catalogs_arc.vert", + r#"#version 300 es +precision highp float; +layout (location = 0) in vec2 offset; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 center; + +uniform float current_time; +uniform mat3 inv_model; + +uniform vec2 ndc_to_clip; +uniform float czf; +uniform vec2 kernel_size; + +out vec2 out_uv; +out vec3 out_p; + +const float PI = 3.141592653589793; +const float SQRT_2 = 1.41421356237309504880168872420969808; + +vec2 w2c_sin(vec3 p) { + vec2 q = vec2(-p.x, p.y); + return p.z >= 0.f ? q : normalize(q); +} + +vec2 w2c_sin_no_backface(vec3 p) { + return vec2(-p.x, p.y); +} + +vec2 w2c_ait(vec3 p) { + float r = length(p.zx); + float w = sqrt((r * (r + p.z)) * 0.5f); // = cos(b) cos(l/2) + w = sqrt((1.0 + w) * 0.5f); // = 1 / gamma + float y2d = p.y / w; + + float x2d = 0.0; + if (abs(p.x) < 5e-3) { + float x_over_r = p.x/r; + x2d = -p.x * (1.0 - x_over_r*x_over_r/21.0) / w; + } else { + w = sqrt((r*r - r*p.z) * 2.0) / w; // = 2 * gamma * cos(b) sin(l/2) + x2d = sign(-p.x) * w; + } + + return vec2(x2d * 0.5, y2d) / SQRT_2; +} +const float eps = 1.25e-8; +const int n_iter = 100; + +float newton_solve(float z) { + float cte = PI * z; + float x = 2.0 * asin(z); + float f = x + sin(x) - cte; + int i = 0; + while (abs(f) > eps && i < n_iter) { + x -= f / (1.0 + cos(x)); + f = x + sin(x) - cte; + i += 1; + } + + return 0.5 * x; +} + +vec2 w2c_mol(vec3 p) { + float g = newton_solve(p.y); + + float sg = sin(g); + float cg = cos(g); + return vec2((atan(-p.x, p.z) * cg) / PI, sg); +} +vec2 w2c_tan(vec3 p) { + p.z = max(p.z, 1e-2); + return vec2(-p.x, p.y) / (p.z*PI); +} +vec2 w2c_stg(vec3 p) { + float w = (1.0 + p.z) * 0.5; + return vec2(-p.x, p.y) / (PI * w); +} +vec2 w2c_zea(vec3 p) { + float w = sqrt(0.5 + 0.5 * p.z); // <=> sqrt[(1 + x) / 2] + return vec2(-p.x, p.y) * 0.5 / w; +} +vec2 w2c_mer(vec3 p) { + return vec2(atan(-p.x, p.z), atanh(p.y)) / PI; +} + +vec3 lonlat2xyz(vec2 lonlat) { + float t = lonlat.x; + float tc = cos(t); + float ts = sin(t); + + float d = lonlat.y; + float dc = cos(d); + float ds = sin(d); + + return vec3(dc * ts, ds, dc * tc); +} + +uniform int u_proj; + +vec2 proj(vec3 p) { + if (u_proj == 0) { + return w2c_tan(p); + } else if (u_proj == 1) { + return w2c_stg(p); + } else if (u_proj == 2) { + return w2c_sin(p); + } else if (u_proj == 3) { + return w2c_zea(p); + } else if (u_proj == 4) { + return w2c_ait(p); + } else if (u_proj == 5) { + return w2c_mol(p); + } else { + return w2c_mer(p); + } +} + +void main() { + vec3 p = inv_model * center; + + vec2 center_pos_clip_space = world2clip_arc(p); + + vec2 pos_clip_space = center_pos_clip_space; + gl_Position = vec4((pos_clip_space / (ndc_to_clip * czf)) + offset * kernel_size , 0.f, 1.f); + + out_uv = uv; + out_p = p; +}"#, + ); + out.insert( + r"colormaps_colormap.vert", + r#"#version 300 es +precision highp float; +precision highp sampler2D; + +layout (location = 0) in vec2 position; +layout (location = 1) in vec2 uv; + +out vec2 out_uv; + +void main() { + gl_Position = vec4(position, 0.f, 1.f); + out_uv = uv; }"#, ); out.insert( @@ -7872,32 +7898,6 @@ void main() { out_frag_color = color; out_frag_color.a = opacity * out_frag_color.a; -}"#, - ); - out.insert( - r"passes_post_fragment_100es.frag", - r#"#version 300 es -precision highp float; - -in vec2 v_tc; -out vec4 color; - -uniform sampler2D fbo_tex; - -vec3 srgb_from_linear(vec3 rgb) { - bvec3 cutoff = lessThan(rgb, vec3(0.0031308)); - vec3 lower = rgb * vec3(3294.6); - vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025); - return mix(higher, lower, vec3(cutoff)); -} - -vec4 srgba_from_linear(vec4 rgba) { - return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a); -} - -void main() { - color = texture(fbo_tex, v_tc); - }"#, ); out diff --git a/src/js/HiPSProperties.js b/src/js/HiPSProperties.js index 8ad64b90..e4316313 100644 --- a/src/js/HiPSProperties.js +++ b/src/js/HiPSProperties.js @@ -186,6 +186,11 @@ HiPSProperties.getFasterMirrorUrl = function (metadata) { mode: 'cors', cache: "no-cache", }; + + if (baseUrl.slice(-1) === '/') { + baseUrl = baseUrl.substr(0, baseUrl.length - 1); + } + let validRequest = await fetch(baseUrl + '/properties', options).then((resp) => { // completed request before timeout fired clearTimeout(timeoutId)