Alternative Checkboard Pattern + Optical Illusion

I saw an optical illusion online and when I recreated it, I stumble upon cool pattern. 🙂

 

Odd number for _Scaling required for the optical illusion to work.

 

To be placed on a square ColorRect for example.

 

For the live preview : Set x and y to 7. and set ColorA to Black.

Shader code
shader_type canvas_item;

uniform vec2 _Scaling = vec2(7.);
uniform vec3 _ColorA : source_color = vec3(0.475, 0.475, 0.651);
uniform vec3 _ColorB : source_color = vec3(0.694, 0.694, 0.878);
uniform bool _DoOpticalIllusion = false;

// Mask for the corner
float corner_mask(vec2 uv, float d){
	float s = uv.x + uv.y;
	bool ul_corner = s < d;
	bool dr_corner = s > 2.-d;
	float t = 1. - uv.x + uv.y;
	bool ur_corner = t < d;
	bool dl_corner = t > 2.-d;
	return float(ul_corner||dr_corner||ur_corner||dl_corner);
}
float dot_mask(vec2 uv, float d, bool invert){
	float s = uv.x + uv.y;
	bool ul_corner = s < d;
	bool ul_dr = invert ? (uv.x < uv.y) : (uv.x > uv.y);
	
	// 90° rotation
	mat2 rot = mat2(vec2(0, 1), vec2(-1, 0));
	
	vec2 new_uv = uv;
	float sum = float(ul_dr&&ul_corner);
	
	for (int i = 0; i < 3; i++){
		new_uv = rot * (new_uv - 0.5) + 0.5;
		bool a = new_uv.x + new_uv.y < d;
		bool b = invert ? (new_uv.x < new_uv.y) : (new_uv.x > new_uv.y);
		sum += float(a&&b);
	}
	return sum;
}
float checkboard_pattern(vec2 uv, vec2 scaling){
	ivec2 i = ivec2(floor(uv * scaling));
	bool chess_board_mask = (i.x + i.y) % 2 == 0;
	return float(chess_board_mask);
}
float center_square_mask(vec2 uv_01, vec2 scaling, float e){
	vec2 s = abs(uv_01 - 0.5) * scaling;
	return float(s.x < scaling.x*0.5 - e) * float(s.y < scaling.y*0.5 - e);
}

void fragment() {
        // Because ivec2 is not supported on godotshader.com
	vec2 scaling = _Scaling;
	const float d = 0.2;
	// Chess pattern
	float chess_mask = checkboard_pattern(UV, scaling);
	vec3 chess_pattern_col = mix(_ColorA, _ColorB, float(chess_mask));
	
	// Border mask / pattern
	float mask = corner_mask(fract(UV * scaling), d);
	float mask_square = center_square_mask(UV, scaling, 0.5);
	float global_mask = 1.-(1.-mask_square) * mask;
	
	// Dot mask
	float dot_pattern = mask * mask_square;
	
	// Dot pattern
	float f = dot_mask(fract(UV * scaling), d, bool(chess_mask));
	vec3 dot_pattern_col = mix(vec3(0.), vec3(1), f);
	if (_DoOpticalIllusion){
		if (UV.x < 0.5) dot_pattern_col = 1.-dot_pattern_col;
		if (UV.y < 0.5) dot_pattern_col = 1.-dot_pattern_col;
	}
	
	COLOR.rgb = mix(chess_pattern_col, dot_pattern_col, dot_pattern);
	COLOR.a = global_mask;
	
	
	//COLOR.rgb = corner_mask(UV, d) * vec3(1);
	//COLOR.rgb = dot_mask(UV, d, true) * vec3(1);
	//COLOR.rgb = vec3(dot_pattern);
}
Live Preview
Tags
checkerboard, Chess, chess pattern, Illusion, Optical, optical illusion, pattern
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Elephando

Related shaders

Comments 0

No comments yet.