forked from VisualComputing/showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/rsierraa/showcase
- Loading branch information
Showing
4 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
let colorShader; | ||
let picker1; | ||
let picker2; | ||
let color1; | ||
let color2; | ||
let rectColor1; | ||
let rectColor2; | ||
let rectResult; | ||
let offset; | ||
let operations = ["mult", "sum", "sub", "overlay", "screen", "darken", "lighten"]; | ||
let effect; | ||
let brightness; | ||
|
||
function preload(){ | ||
colorShader = readShader('/showcase/sketches/shaders/Blending/colorBlender2.frag', | ||
{varyings: Tree.color4}); | ||
} | ||
|
||
function setup() { | ||
createCanvas(600, 600, WEBGL); | ||
|
||
picker1 = createColorPicker('red'); | ||
picker1.position(width/8, height/8); | ||
picker2 = createColorPicker('lightgreen'); | ||
picker2.position(width*7/12, height/8); | ||
|
||
effect = createSelect(); | ||
effect.position(10, 10); | ||
effect.option('effect'); | ||
for(let i of operations) effect.option(i); | ||
effect.changed(changeEffect); | ||
colorShader.setUniform('mult', true); | ||
|
||
brightness = createSlider(0, 1, 0.5, 0); | ||
brightness.position(300-40, 260); | ||
brightness.style('width', '80px'); | ||
|
||
rectColor1 = createGraphics(150, 150); | ||
rectColor2 = createGraphics(150, 150); | ||
rectResult = createGraphics(1, 1); | ||
|
||
color1 = picker1.value(); | ||
color2 = picker2.value(); | ||
|
||
offset = 40; | ||
} | ||
|
||
function draw() { | ||
background('#1E1E37'); | ||
|
||
color1 = picker1.value(); | ||
color2 = picker2.value(); | ||
|
||
rectColor1.background(color1); | ||
rectColor2.background(color2); | ||
image(rectColor1, -width/2+offset*2, -height/2+offset*2); | ||
image(rectColor2, -width/2+offset*9, -height/2+offset*2); | ||
|
||
shader(colorShader); | ||
colorShader.setUniform('brightness', brightness.value()); | ||
colorShader.setUniform('color1', normalized(color1)); | ||
colorShader.setUniform('color2', normalized(color2)); | ||
|
||
beginShape(); | ||
vertex(-1/3, -2/3); | ||
vertex(1/3, -2/3); | ||
vertex(1/3, 0.0); | ||
vertex(-1/3, 0.0); | ||
endShape(); | ||
} | ||
|
||
function normalized(color){ | ||
const r = red(color)/255; | ||
const g = green(color)/255; | ||
const b = blue(color)/255; | ||
return [r,g,b]; | ||
} | ||
|
||
function changeEffect(){ | ||
effect.disable('effect'); | ||
for(let i of operations) colorShader.setUniform(i, false); | ||
colorShader.setUniform(effect.value(), true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
precision mediump float; | ||
|
||
uniform vec3 color1; | ||
uniform vec3 color2; | ||
|
||
uniform float brightness; | ||
|
||
uniform bool mult; | ||
uniform bool sum; | ||
uniform bool sub; | ||
uniform bool overlay; | ||
uniform bool screen; | ||
uniform bool darken; | ||
uniform bool lighten; | ||
|
||
|
||
void main() { | ||
|
||
float r1 = color1.r; | ||
float g1 = color1.g; | ||
float b1 = color1.b; | ||
|
||
float r2 = color2.r; | ||
float g2 = color2.g; | ||
float b2 = color2.b; | ||
|
||
vec3 result = vec3(0.0); | ||
|
||
if(mult){ | ||
result = (color1*color2); | ||
}else if(sum){ | ||
result = (color1+color2); | ||
}else if(sub){ | ||
result = (color1-color2); | ||
}else if(overlay){ | ||
result = r1<=0.5 ? result+vec3(2.0*r1+r2, 0.0, 0.0) : result+vec3(1.0-2.0*(1.0-r1)*(1.0-r2), 0.0, 0.0); | ||
result = g1<=0.5 ? result+vec3(0.0, 2.0*g1+g2, 0.0) : result+vec3(0.0, 1.0-2.0*(1.0-g1)*(1.0-g2), 0.0); | ||
result = b1<=0.5 ? result+vec3(0.0, 0.0, 2.0*b1+b2) : result+vec3(0.0, 0.0, 1.0-2.0*(1.0-b1)*(1.0-b2)); | ||
}else if(screen){ | ||
result = vec3(1.0-(1.0-r1)*(1.0-r2), 1.0-(1.0-g1)*(1.0-g2), 1.0-(1.0-b1)*(1.0-b2)); | ||
}else if(darken){ | ||
result = vec3(min(r1, r2), min(g1, g2), min(b1, b2)); | ||
}else if(lighten){ | ||
result = vec3(max(r1, r2), max(g1, g2), max(b1, b2)); | ||
} | ||
|
||
gl_FragColor = vec4(brightness*result, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters