Add checkers pattern

This commit is contained in:
Edwin Jakobs
2020-03-13 22:48:13 +01:00
parent 3d45310961
commit 8e94094d05
2 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package org.openrndr.extra.fx.patterns
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.Filter
import org.openrndr.draw.Shader
import org.openrndr.extra.fx.filterFragmentCode
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
@Description("Checkers pattern")
class Checkers : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("patterns/checkers.frag"))) {
var background: ColorRGBa by parameters
var foreground: ColorRGBa by parameters
@DoubleParameter("size", 0.0, 1.0)
var size: Double by parameters
@DoubleParameter("opacity", 0.0, 1.0)
var opacity: Double by parameters
init {
size = 1.0/64.0
opacity = 1.0
foreground = ColorRGBa.WHITE.shade(0.9)
background = ColorRGBa.WHITE.shade(0.8)
}
}

View File

@@ -0,0 +1,36 @@
#version 330
in vec2 v_texCoord0;
uniform vec4 foreground;
uniform vec4 background;
uniform vec2 targetSize;
uniform float size;
uniform float opacity;
out vec4 o_color;
void main() {
float r = targetSize.x/targetSize.y;
vec2 uv = v_texCoord0-vec2(0.5);
uv.x *= r;
vec2 cell = (uv / size);
ivec2 cellIndex = ivec2(floor(cell));
vec2 cellUV = cell - cellIndex;
int c = (cellIndex.x + cellIndex.y) % 2;
vec2 w = fwidth(cell);
vec4 ca;
vec4 cb;
if (c == 0) {
ca = background;
cb = foreground;
} else {
ca = foreground;
cb = background;
}
float s = w.x;
float fx = smoothstep(s, 0.0, cellUV.x) + smoothstep(1.0-s, 1.0, cellUV.x);
float fy = smoothstep(s, 0.0, cellUV.y) + smoothstep(1.0-s, 1.0, cellUV.y);
o_color = mix(ca, cb, min(0.5, fx*0.5+ fy*0.5)) * opacity;
}