From 16305cd3fc7347d4dad12b60610c335aadcec0f9 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 17 Aug 2024 12:24:04 +0200 Subject: [PATCH 1/2] cleanup --- Assets/test/QRCode.shader | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/Assets/test/QRCode.shader b/Assets/test/QRCode.shader index a03269c..f6e0b77 100644 --- a/Assets/test/QRCode.shader +++ b/Assets/test/QRCode.shader @@ -1,8 +1,8 @@ Shader "CrispyPin/QRCode" { Properties { [HideInInspector] - _("",2D)="" - _Version("Version", Integer) = 1 + _("",2D)=""{} + _Version("Version", Range(1, 40)) = 1 } SubShader { Tags { "RenderType"="Opaque" } @@ -14,6 +14,8 @@ CGPROGRAM #define VERSION _Version #define WIDTH (17 + VERSION * 4) + #define PIXEL_WIDTH WIDTH + // #define PIXEL_WIDTH 177 #define ALIGNERS ((VERSION / 7) + 2) #define ALIGNER_SPACING_IDEAL ((WIDTH - 13) / (ALIGNERS-1)) #define ALIGNER_SPACING (ALIGNER_SPACING_IDEAL + ALIGNER_SPACING_IDEAL % 2) @@ -40,24 +42,23 @@ CGPROGRAM #define WHITE 1 #define BLACK 0 - #define PINK float4(1, .3, .5, 1) - #define BLUE float4(0, .4, .7, 1) + #define PINK float3(1, .3, .5) + #define BLUE float3(0, .4, .7) - float4 finder_pattern(uint x, uint y) { + float3 finder_pattern(uint x, uint y) { if (x < 6 && x > 0 && y < 6 && y > 0) return !(x < 5 && x > 1 && y < 5 && y > 1); return (x > 6 || y > 6); } - float4 main (float2 uv){ + float3 main (float2 uv){ const uint data[] = {0x68656c6c,0x6f20776f,0x726c6421}; uv.y = 1 - uv.y; uv = uv * 1.5 - 0.25; // Quiet zone if (uv.x < 0 || uv.x > 1 || uv.y < 0 || uv.y > 1) return 1; - uint px = uv.x * WIDTH; - uint py = uv.y * WIDTH; - + uint px = uv.x * PIXEL_WIDTH; + uint py = uv.y * PIXEL_WIDTH; // Finder patterns if (px < 8 && py < 8) return finder_pattern(px, py); @@ -77,14 +78,14 @@ CGPROGRAM && (px < WIDTH - 13 || py > 12) // top right && (py < WIDTH - 13 || px > 12) // bottom left ) { - uint x = px + ALIGNER_SPACING - 4 - MISALIGNMENT; - uint y = py + ALIGNER_SPACING - 4 - MISALIGNMENT; - if (px < 15) x += MISALIGNMENT; - if (py < 15) y += MISALIGNMENT; - uint ax = x % ALIGNER_SPACING; - uint ay = y % ALIGNER_SPACING; - if (ax < 5 && ay < 5) { - return (ax < 4 && ax > 0 && ay < 4 && ay > 0 && !(ax == 2 && ay == 2)); + uint x = px + ALIGNER_SPACING - 4; + uint y = py + ALIGNER_SPACING - 4; + if (px > 12) x -= MISALIGNMENT; + if (py > 12) y -= MISALIGNMENT; + x %= ALIGNER_SPACING; + y %= ALIGNER_SPACING; + if (x < 5 && y < 5) { + return (x < 4 && x > 0 && y < 4 && y > 0 && !(x == 2 && y == 2)); } } @@ -104,7 +105,8 @@ CGPROGRAM uint direction_up = column & 1; // return lerp(BLUE, PINK, column / (WIDTH/2.)); return lerp(BLUE, PINK, (px+py)&1); - // return float4((px/8 + py/8) & 1, (px/2 + py/2) & 1, (px/4 + py/4) & 1, 1); + // return float3((px%5)/5., (px/10+py/10)&1, (py%5)/5.); + // return float3((px/8 + py/8) & 1, (px/2 + py/2) & 1, (px/4 + py/4) & 1, 1); // return direction_up; return 0.5; } From d470f0098721e2bed8c9fc4f8cf6366954839ea3 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 17 Aug 2024 14:24:02 +0200 Subject: [PATCH 2/2] qr shader: determine bit order for size 1 (ignores aligners at larger sizes) --- Assets/test/QRCode.mat | 2 ++ Assets/test/QRCode.shader | 36 ++++++++++++++++++++++++++++++++++-- Assets/test/TestProps.unity | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Assets/test/QRCode.mat b/Assets/test/QRCode.mat index 68b50ec..c9b6cac 100644 --- a/Assets/test/QRCode.mat +++ b/Assets/test/QRCode.mat @@ -77,7 +77,9 @@ Material: - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 - _SrcBlend: 1 + - _TimeSlider: 0 - _UVSec: 0 + - _Version: 1 - _ZWrite: 1 m_Colors: - _Color: {r: 1, g: 1, b: 1, a: 1} diff --git a/Assets/test/QRCode.shader b/Assets/test/QRCode.shader index f6e0b77..3645021 100644 --- a/Assets/test/QRCode.shader +++ b/Assets/test/QRCode.shader @@ -3,6 +3,7 @@ Properties { [HideInInspector] _("",2D)=""{} _Version("Version", Range(1, 40)) = 1 + _TimeSlider("Time", Range(0, 250)) = 0 } SubShader { Tags { "RenderType"="Opaque" } @@ -11,6 +12,7 @@ CGPROGRAM #pragma surface s Standard struct Input{float2 uv_;}; uint _Version; + float _TimeSlider; #define VERSION _Version #define WIDTH (17 + VERSION * 4) @@ -20,6 +22,9 @@ CGPROGRAM #define ALIGNER_SPACING_IDEAL ((WIDTH - 13) / (ALIGNERS-1)) #define ALIGNER_SPACING (ALIGNER_SPACING_IDEAL + ALIGNER_SPACING_IDEAL % 2) #define MISALIGNMENT ((WIDTH - 13) - ALIGNER_SPACING * ALIGNERS) + #define BIT_COUNT (WIDTH * WIDTH - 225 - (WIDTH - 17)*2) + #define TIME (_Time.y*24) + // #define TIME (uint)(_TimeSlider) #define EC_LEVEL_L 1 #define EC_LEVEL_M 0 @@ -44,6 +49,7 @@ CGPROGRAM #define BLACK 0 #define PINK float3(1, .3, .5) #define BLUE float3(0, .4, .7) + #define CYAN float3(0,1,1) float3 finder_pattern(uint x, uint y) { if (x < 6 && x > 0 && y < 6 && y > 0) return !(x < 5 && x > 1 && y < 5 && y > 1); @@ -101,8 +107,34 @@ CGPROGRAM // bottom if (px == 8 && py > WIDTH - 8) return (FORMAT_BITS & (1 << py + 15 - WIDTH)) == 0; - uint column = (WIDTH - px - (px > 6))/2; - uint direction_up = column & 1; + // data bit layout + uint column = (WIDTH - px - (px < 6)-1)/2; + uint direction_up = column % 2 == 0; + uint full_columns = column > 4 ? column - 4 : 0; + full_columns = min(full_columns, VERSION*2); + // the 4 columns under the right finder + uint short_columns = min(column, 4); + // between the left two finder patterns + uint tiny_columns = column > (VERSION*2 + 4) ? column - (VERSION*2 + 4) : 0; + uint bit_index = full_columns * (WIDTH - 1) * 2; + bit_index += short_columns * (WIDTH - 9) * 2; + bit_index += tiny_columns * (WIDTH - 17) * 2; + bit_index += (px + (px < 6)) % 2; + + if (direction_up) { + bit_index += (WIDTH - py - 1 - (py < 7)) * 2; + } else {// direction down + const uint diff = (full_columns == 0) ? 9 : (py > 6); + bit_index += (py - diff)*2; + } + if (px < 9) bit_index -= 16; // rightmost tiny column + + // debugging worm + uint worm = abs(TIME % BIT_COUNT - bit_index); + const uint length = 4; + if (worm < length) return CYAN * (worm/(float)length); + return (float)(bit_index%BIT_COUNT)/(float)BIT_COUNT; + // return lerp(BLUE, PINK, column / (WIDTH/2.)); return lerp(BLUE, PINK, (px+py)&1); // return float3((px%5)/5., (px/10+py/10)&1, (py%5)/5.); diff --git a/Assets/test/TestProps.unity b/Assets/test/TestProps.unity index 982416b..e6c90d1 100644 --- a/Assets/test/TestProps.unity +++ b/Assets/test/TestProps.unity @@ -1360,7 +1360,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1500710831} m_LocalRotation: {x: 0, y: 1, z: 0, w: 0} - m_LocalPosition: {x: 0, y: 0.45, z: 1} + m_LocalPosition: {x: 0, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: []