From ee38da6176bce035e6059bf2046c73f6493ab166 Mon Sep 17 00:00:00 2001 From: Ondrej Jamriska Date: Wed, 21 Sep 2022 05:04:32 +0200 Subject: [PATCH] add mixbox shaders --- shaders/README.md | 100 +++++++++++++ shaders/mixbox.glsl | 173 +++++++++++++++++++++++ shaders/mixbox.hlsl | 155 ++++++++++++++++++++ mixbox_lut.png => shaders/mixbox_lut.png | Bin 4 files changed, 428 insertions(+) create mode 100644 shaders/README.md create mode 100644 shaders/mixbox.glsl create mode 100644 shaders/mixbox.hlsl rename mixbox_lut.png => shaders/mixbox_lut.png (100%) diff --git a/shaders/README.md b/shaders/README.md new file mode 100644 index 0000000..ab3b7e9 --- /dev/null +++ b/shaders/README.md @@ -0,0 +1,100 @@ +## GLSL Shader +```glsl +#ifdef GL_ES +precision highp float; +#endif + +// uncomment the following line if you work in linear space +// #define MIXBOX_COLORSPACE_LINEAR + +uniform sampler2D mixbox_lut; // bind the "mixbox_lut.png" texture here + +#include "mixbox.glsl" // paste the contents of mixbox.glsl here + +void main(void) +{ + vec3 rgb1 = vec3(0, 0.129, 0.522); // blue + vec3 rgb2 = vec3(0.988, 0.827, 0); // yellow + float t = 0.5; // mixing ratio + + vec3 rgb = mixbox_lerp(rgb1, rgb2, t); + + gl_FragColor = vec4(rgb, 1.0); +} +``` +```glsl +vec3 mix_three(vec3 rgb1, vec3 rgb2, vec3 rgb3) +{ + mixbox_latent z1 = mixbox_rgb_to_latent(rgb1); + mixbox_latent z2 = mixbox_rgb_to_latent(rgb2); + mixbox_latent z3 = mixbox_rgb_to_latent(rgb3); + + // mix together 30% of rgb1, 60% of rgb2, and 10% of rgb3 + mixbox_latent z_mix = 0.3*z1 + 0.6*z2 + 0.1*z3; + + vec3 rgb_mix = mixbox_latent_to_rgb(z_mix); + + return rgb_mix; +} +``` + +## HLSL Shader +```hlsl +// uncomment the following line if you work in linear space +// #define MIXBOX_COLORSPACE_LINEAR + +Texture2D MixboxLUT; // bind the "mixbox_lut.png" texture here +SamplerState MixboxSampler; // FILTER_MIN_MAG_LINEAR_MIP_POINT + +#define MIXBOX_LUT(UV) MixboxLUT.SampleLevel(MixboxSampler, UV, 0) + +#include "mixbox.hlsl" + +float4 PSMain() : SV_Target +{ + float3 rgb1 = float3(0, 0.129, 0.522); // blue + float3 rgb2 = float3(0.988, 0.827, 0); // yellow + float t = 0.5; // mixing ratio + + float3 rgb_mix = MixboxLerp(rgb1, rgb2, t); + + return float4(rgb_mix, 1.0); +} +``` +```hlsl +float3 MixThree(float3 rgb1, float3 rgb2, float3 rgb3) +{ + MixboxLatent z1 = MixboxRGBToLatent(rgb1); + MixboxLatent z2 = MixboxRGBToLatent(rgb2); + MixboxLatent z3 = MixboxRGBToLatent(rgb3); + + // mix together 30% of rgb1, 60% of rgb2, and 10% of rgb3 + MixboxLatent zMix = 0.3*z1 + 0.6*z2 + 0.1*z3; + + float3 rgbMix = MixboxLatentToRGB(zMix); + + return rgbMix; +} +``` + +## Pigment Colors +| Pigment | | RGB | Float RGB | Linear RGB | +| --- | --- |:----:|:----:|:----:| +| Cadmium Yellow | | 254, 236, 0 | 0.996, 0.925, 0.0 | 0.991, 0.839, 0.0 | +| Hansa Yellow | | 252, 211, 0 | 0.988, 0.827, 0.0 | 0.973, 0.651, 0.0 | +| Cadmium Orange | | 255, 105, 0 | 1.0, 0.412, 0.0 | 1.0, 0.141, 0.0 | +| Cadmium Red | | 255, 39, 2 | 1.0, 0.153, 0.008 | 1.0, 0.02, 0.001 | +| Quinacridone Magenta | | 128, 2, 46 | 0.502, 0.008, 0.18 | 0.216, 0.001, 0.027 | +| Cobalt Violet | | 78, 0, 66 | 0.306, 0.0, 0.259 | 0.076, 0.0, 0.054 | +| Ultramarine Blue | | 25, 0, 89 | 0.098, 0.0, 0.349 | 0.01, 0.0, 0.1 | +| Cobalt Blue | | 0, 33, 133 | 0.0, 0.129, 0.522 | 0.0, 0.015, 0.235 | +| Phthalo Blue | | 13, 27, 68 | 0.051, 0.106, 0.267 | 0.004, 0.011, 0.058 | +| Phthalo Green | | 0, 60, 50 | 0.0, 0.235, 0.196 | 0.0, 0.045, 0.032 | +| Permanent Green | | 7, 109, 22 | 0.027, 0.427, 0.086 | 0.002, 0.153, 0.008 | +| Sap Green | | 107, 148, 4 | 0.42, 0.58, 0.016 | 0.147, 0.296, 0.001 | +| Burnt Sienna | | 123, 72, 0 | 0.482, 0.282, 0.0 | 0.198, 0.065, 0.0 | + +## License +Copyright (c) 2022, Secret Weapons. All rights reserved.
+Mixbox is provided under the CC BY-NC 4.0 license for non-commercial use only.
+If you want to obtain commercial license, please contact: mixbox@scrtwpns.com diff --git a/shaders/mixbox.glsl b/shaders/mixbox.glsl new file mode 100644 index 0000000..5ce2957 --- /dev/null +++ b/shaders/mixbox.glsl @@ -0,0 +1,173 @@ +// ========================================================== +// MIXBOX 2.0 (c) 2022 Secret Weapons. All rights reserved. +// License: Creative Commons Attribution-NonCommercial 4.0 +// Authors: Sarka Sochorova and Ondrej Jamriska +// ========================================================== +// +// BASIC USAGE +// +// vec3 rgb = mixbox_lerp(rgb1, rgb2, t); +// +// MULTI-COLOR MIXING +// +// mixbox_latent z1 = mixbox_rgb_to_latent(rgb1); +// mixbox_latent z2 = mixbox_rgb_to_latent(rgb2); +// mixbox_latent z3 = mixbox_rgb_to_latent(rgb3); +// +// // mix 30% of rgb1, 60% of rgb2, and 10% of rgb3 +// mixbox_latent z_mix = 0.3*z1 + 0.6*z2 + 0.1*z3; +// +// vec3 rgb_mix = mixbox_latent_to_rgb(z_mix); +// +// PIGMENT COLORS +// +// Cadmium Yellow 0.996, 0.925, 0.000 +// Hansa Yellow 0.988, 0.827, 0.000 +// Cadmium Orange 1.000, 0.412, 0.000 +// Cadmium Red 1.000, 0.153, 0.008 +// Quinacridone Magenta 0.502, 0.008, 0.180 +// Cobalt Violet 0.306, 0.000, 0.259 +// Ultramarine Blue 0.098, 0.000, 0.349 +// Cobalt Blue 0.000, 0.129, 0.522 +// Phthalo Blue 0.051, 0.106, 0.267 +// Phthalo Green 0.000, 0.235, 0.196 +// Permanent Green 0.027, 0.427, 0.086 +// Sap Green 0.420, 0.580, 0.016 +// Burnt Sienna 0.482, 0.282, 0.000 +// +// LICENSING +// +// If you want to obtain commercial license, please +// contact us at: mixbox@scrtwpns.com +// + +#ifndef MIXBOX_INCLUDED +#define MIXBOX_INCLUDED + +#ifndef MIXBOX_LUT + #if __VERSION__ <= 120 + #define MIXBOX_LUT(UV) texture2D(mixbox_lut, UV) + #else + #define MIXBOX_LUT(UV) textureLod(mixbox_lut, UV, 0.0) + #endif +#endif + +#define mixbox_latent mat3 + +vec3 mixbox_eval_polynomial(vec3 c) +{ + float c0 = c[0]; + float c1 = c[1]; + float c2 = c[2]; + float c3 = 1.0 - (c0 + c1 + c2); + + float c00 = c0 * c0; + float c11 = c1 * c1; + float c22 = c2 * c2; + float c01 = c0 * c1; + float c02 = c0 * c2; + float c12 = c1 * c2; + float c33 = c3 * c3; + + return (c0*c00) * vec3(+0.07717053, +0.02826978, +0.24832992) + + (c1*c11) * vec3(+0.95912302, +0.80256528, +0.03561839) + + (c2*c22) * vec3(+0.74683774, +0.04868586, +0.00000000) + + (c3*c33) * vec3(+0.99518138, +0.99978149, +0.99704802) + + (c00*c1) * vec3(+0.04819146, +0.83363781, +0.32515377) + + (c01*c1) * vec3(-0.68146950, +1.46107803, +1.06980936) + + (c00*c2) * vec3(+0.27058419, -0.15324870, +1.98735057) + + (c02*c2) * vec3(+0.80478189, +0.67093710, +0.18424500) + + (c00*c3) * vec3(-0.35031003, +1.37855826, +3.68865000) + + (c0*c33) * vec3(+1.05128046, +1.97815239, +2.82989073) + + (c11*c2) * vec3(+3.21607125, +0.81270228, +1.03384539) + + (c1*c22) * vec3(+2.78893374, +0.41565549, -0.04487295) + + (c11*c3) * vec3(+3.02162577, +2.55374103, +0.32766114) + + (c1*c33) * vec3(+2.95124691, +2.81201112, +1.17578442) + + (c22*c3) * vec3(+2.82677043, +0.79933038, +1.81715262) + + (c2*c33) * vec3(+2.99691099, +1.22593053, +1.80653661) + + (c01*c2) * vec3(+1.87394106, +2.05027182, -0.29835996) + + (c01*c3) * vec3(+2.56609566, +7.03428198, +0.62575374) + + (c02*c3) * vec3(+4.08329484, -1.40408358, +2.14995522) + + (c12*c3) * vec3(+6.00078678, +2.55552042, +1.90739502); +} + +float mixbox_srgb_to_linear(float x) +{ + return (x >= 0.04045) ? pow((x + 0.055) / 1.055, 2.4) : x/12.92; +} + +float mixbox_linear_to_srgb(float x) +{ + return (x >= 0.0031308) ? 1.055*pow(x, 1.0/2.4) - 0.055 : 12.92*x; +} + +vec3 mixbox_srgb_to_linear(vec3 rgb) +{ + return vec3(mixbox_srgb_to_linear(rgb.r), + mixbox_srgb_to_linear(rgb.g), + mixbox_srgb_to_linear(rgb.b)); +} + +vec3 mixbox_linear_to_srgb(vec3 rgb) +{ + return vec3(mixbox_linear_to_srgb(rgb.r), + mixbox_linear_to_srgb(rgb.g), + mixbox_linear_to_srgb(rgb.b)); +} + +mixbox_latent mixbox_rgb_to_latent(vec3 rgb) +{ +#ifdef MIXBOX_COLORSPACE_LINEAR + rgb = mixbox_linear_to_srgb(clamp(rgb, 0.0, 1.0)); +#else + rgb = clamp(rgb, 0.0, 1.0); +#endif + + float x = rgb.r * 63.0; + float y = rgb.g * 63.0; + float z = rgb.b * 63.0; + + float iz = floor(z); + + float x0 = mod(iz, 8.0) * 64.0; + float y0 = floor(iz / 8.0) * 64.0; + + float x1 = mod(iz + 1.0, 8.0) * 64.0; + float y1 = floor((iz + 1.0) / 8.0) * 64.0; + + vec2 uv0 = vec2(x0 + x + 0.5, y0 + y + 0.5) / 512.0; + vec2 uv1 = vec2(x1 + x + 0.5, y1 + y + 0.5) / 512.0; + + if (MIXBOX_LUT(vec2(0.5, 0.5) / 512.0).b < 0.1) + { + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + } + + vec3 c = mix(MIXBOX_LUT(uv0).rgb, MIXBOX_LUT(uv1).rgb, z - iz); + + return mixbox_latent(c, rgb - mixbox_eval_polynomial(c), vec3(0.0)); +} + +vec3 mixbox_latent_to_rgb(mixbox_latent latent) +{ + vec3 rgb = clamp(mixbox_eval_polynomial(latent[0]) + latent[1], 0.0, 1.0); + +#ifdef MIXBOX_COLORSPACE_LINEAR + return mixbox_srgb_to_linear(rgb); +#else + return rgb; +#endif +} + +vec3 mixbox_lerp(vec3 color1, vec3 color2, float t) +{ + return mixbox_latent_to_rgb((1.0-t)*mixbox_rgb_to_latent(color1) + t*mixbox_rgb_to_latent(color2)); +} + +vec4 mixbox_lerp(vec4 color1, vec4 color2, float t) +{ + return vec4(mixbox_lerp(color1.rgb, color2.rgb, t), mix(color1.a, color2.a, t)); +} + +#endif diff --git a/shaders/mixbox.hlsl b/shaders/mixbox.hlsl new file mode 100644 index 0000000..04491ef --- /dev/null +++ b/shaders/mixbox.hlsl @@ -0,0 +1,155 @@ +// ========================================================== +// MIXBOX 2.0 (c) 2022 Secret Weapons. All rights reserved. +// License: Creative Commons Attribution-NonCommercial 4.0 +// Authors: Sarka Sochorova and Ondrej Jamriska +// ========================================================== +// +// BASIC USAGE +// +// float3 rgb = MixboxLerp(rgb1, rgb2, t); +// +// MULTI-COLOR MIXING +// +// MixboxLatent z1 = MixboxRGBToLatent(rgb1); +// MixboxLatent z2 = MixboxRGBToLatent(rgb2); +// MixboxLatent z3 = MixboxRGBToLatent(rgb3); +// +// // mix 30% of rgb1, 60% of rgb2, and 10% of rgb3 +// MixboxLatent z_mix = 0.3*z1 + 0.6*z2 + 0.1*z3; +// +// float3 rgb_mix = MixboxLatentToRGB(z_mix); +// +// PIGMENT COLORS +// +// Cadmium Yellow 0.996, 0.925, 0.000 +// Hansa Yellow 0.988, 0.827, 0.000 +// Cadmium Orange 1.000, 0.412, 0.000 +// Cadmium Red 1.000, 0.153, 0.008 +// Quinacridone Magenta 0.502, 0.008, 0.180 +// Cobalt Violet 0.306, 0.000, 0.259 +// Ultramarine Blue 0.098, 0.000, 0.349 +// Cobalt Blue 0.000, 0.129, 0.522 +// Phthalo Blue 0.051, 0.106, 0.267 +// Phthalo Green 0.000, 0.235, 0.196 +// Permanent Green 0.027, 0.427, 0.086 +// Sap Green 0.420, 0.580, 0.016 +// Burnt Sienna 0.482, 0.282, 0.000 +// +// LICENSING +// +// If you want to obtain commercial license, please +// contact us at: mixbox@scrtwpns.com +// + +#ifndef MIXBOX_INCLUDED +#define MIXBOX_INCLUDED + +#ifndef MIXBOX_LUT + #define MIXBOX_LUT(UV) MixboxLUT.SampleLevel(MixboxSampler, UV, 0) +#endif + +typedef float3x3 MixboxLatent; + +float3 MixboxEvalPolynomial(float3 c) +{ + float c0 = c[0]; + float c1 = c[1]; + float c2 = c[2]; + float c3 = 1.0 - (c0 + c1 + c2); + + float c00 = c0 * c0; + float c11 = c1 * c1; + float c22 = c2 * c2; + float c01 = c0 * c1; + float c02 = c0 * c2; + float c12 = c1 * c2; + float c33 = c3 * c3; + + return (c0*c00) * float3(+0.07717053, +0.02826978, +0.24832992) + + (c1*c11) * float3(+0.95912302, +0.80256528, +0.03561839) + + (c2*c22) * float3(+0.74683774, +0.04868586, +0.00000000) + + (c3*c33) * float3(+0.99518138, +0.99978149, +0.99704802) + + (c00*c1) * float3(+0.04819146, +0.83363781, +0.32515377) + + (c01*c1) * float3(-0.68146950, +1.46107803, +1.06980936) + + (c00*c2) * float3(+0.27058419, -0.15324870, +1.98735057) + + (c02*c2) * float3(+0.80478189, +0.67093710, +0.18424500) + + (c00*c3) * float3(-0.35031003, +1.37855826, +3.68865000) + + (c0*c33) * float3(+1.05128046, +1.97815239, +2.82989073) + + (c11*c2) * float3(+3.21607125, +0.81270228, +1.03384539) + + (c1*c22) * float3(+2.78893374, +0.41565549, -0.04487295) + + (c11*c3) * float3(+3.02162577, +2.55374103, +0.32766114) + + (c1*c33) * float3(+2.95124691, +2.81201112, +1.17578442) + + (c22*c3) * float3(+2.82677043, +0.79933038, +1.81715262) + + (c2*c33) * float3(+2.99691099, +1.22593053, +1.80653661) + + (c01*c2) * float3(+1.87394106, +2.05027182, -0.29835996) + + (c01*c3) * float3(+2.56609566, +7.03428198, +0.62575374) + + (c02*c3) * float3(+4.08329484, -1.40408358, +2.14995522) + + (c12*c3) * float3(+6.00078678, +2.55552042, +1.90739502); +} + +float3 MixboxSRGBToLinear(float3 rgb) +{ + return (rgb >= 0.04045) ? pow((abs(rgb) + 0.055) / 1.055, 2.4) : rgb/12.92; +} + +float3 MixboxLinearToSRGB(float3 rgb) +{ + return (rgb >= 0.0031308) ? 1.055*pow(abs(rgb), 1.0/2.4) - 0.055 : 12.92*rgb; +} + +MixboxLatent MixboxRGBToLatent(float3 rgb) +{ +#ifdef MIXBOX_COLORSPACE_LINEAR + rgb = MixboxLinearToSRGB(saturate(rgb)); +#else + rgb = saturate(rgb); +#endif + + float x = rgb.r * 63.0; + float y = rgb.g * 63.0; + float z = rgb.b * 63.0; + + float iz = floor(z); + + float x0 = fmod(iz, 8.0) * 64.0; + float y0 = floor(iz / 8.0) * 64.0; + + float x1 = fmod(iz + 1.0, 8.0) * 64.0; + float y1 = floor((iz + 1.0) / 8.0) * 64.0; + + float2 uv0 = float2(x0 + x + 0.5, 512.0 - (y0 + y + 0.5)) / 512.0; + float2 uv1 = float2(x1 + x + 0.5, 512.0 - (y1 + y + 0.5)) / 512.0; + + if (MIXBOX_LUT(float2(0.5, 0.5) / 512.0).b > 0.1) + { + uv0.y = 1.0 - uv0.y; + uv1.y = 1.0 - uv1.y; + } + + float3 c = lerp(MIXBOX_LUT(uv0).rgb, MIXBOX_LUT(uv1).rgb, z - iz); + + return MixboxLatent(c, rgb - MixboxEvalPolynomial(c), 0.0, 0.0, 0.0); +} + +float3 MixboxLatentToRGB(MixboxLatent latent) +{ + float3 rgb = saturate(MixboxEvalPolynomial(latent[0]) + latent[1]); + +#ifdef MIXBOX_COLORSPACE_LINEAR + return MixboxSRGBToLinear(rgb); +#else + return rgb; +#endif +} + +float3 MixboxLerp(float3 color1, float3 color2, float t) +{ + return MixboxLatentToRGB((1.0-t)*MixboxRGBToLatent(color1) + t*MixboxRGBToLatent(color2)); +} + +float4 MixboxLerp(float4 color1, float4 color2, float t) +{ + return float4(MixboxLerp(color1.rgb, color2.rgb, t), lerp(color1.a, color2.a, t)); +} + +#endif diff --git a/mixbox_lut.png b/shaders/mixbox_lut.png similarity index 100% rename from mixbox_lut.png rename to shaders/mixbox_lut.png