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.
- Loading branch information
Showing
7 changed files
with
123 additions
and
23 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,24 @@ | ||
precision mediump float; | ||
|
||
// uniforms are defined and sent by the sketch | ||
uniform bool lightness; | ||
uniform sampler2D texture; | ||
uniform bool uv; // uv visualization | ||
|
||
// texture space normalized interpolated texture coordinates | ||
// should have same name and type as in vertex shader | ||
varying vec2 texcoords2; // (defined in [0..1] ∈ R) | ||
|
||
// returns luma of given texel | ||
float luma(vec4 texel) { | ||
// alpha channel (texel.a) is just discarded | ||
return 0.299 * texel.r + 0.587 * texel.g + 0.114 * texel.b; | ||
} | ||
|
||
void main() { | ||
// texture2D(texture, texcoords2) samples texture at texcoords2 | ||
// and returns the normalized texel color | ||
vec4 texel = texture2D(texture, texcoords2); | ||
gl_FragColor = uv ? vec4(texcoords2.st, 0.0, 1.0) : | ||
lightness ? vec4(vec3(luma(texel)), 1.0) : texel; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,66 @@ | ||
let lumaShader; | ||
let src; | ||
let img_src; | ||
let video_src; | ||
let video_on; | ||
let lightness; | ||
let uv; | ||
|
||
function preload() { | ||
lumaShader = readShader('/showcase/docs/Shaders/fragments/luma.frag', | ||
{ varyings: Tree.texcoords2 }); | ||
// video source: https://t.ly/LWUs2 | ||
video_src = createVideo(['/showcase/docs/Shaders/resources/video0.mp4']); | ||
video_src.hide(); // by default video shows up in separate dom | ||
// image source: https://t.ly/Dz8W | ||
img_src = loadImage('/showcase/docs/Shaders/resources/photomosaic.jpg'); | ||
src = img_src; | ||
} | ||
|
||
function setup() { | ||
createCanvas(700, 500, WEBGL); | ||
noStroke(); | ||
textureMode(NORMAL); | ||
shader(lumaShader); | ||
video_on = createCheckbox('video', false); | ||
video_on.style('color', 'white'); | ||
video_on.changed(() => { | ||
src = video_on.checked() ? video_src : img_src; | ||
video_on.checked() ? video_src.loop() : video_src.pause(); | ||
}); | ||
video_on.position(10, 10); | ||
lightness = createCheckbox('luma', false); | ||
lightness.position(10, 30); | ||
lightness.style('color', 'white'); | ||
lightness.input(() => lumaShader.setUniform('lightness', lightness.checked())); | ||
uv = createCheckbox('uv visualization', false); | ||
uv.style('color', 'white'); | ||
uv.changed(() => lumaShader.setUniform('uv', uv.checked())); | ||
uv.position(10, 50); | ||
} | ||
|
||
function draw() { | ||
/* | ||
NDC quad shape, i.e., x, y and z vertex coordinates ∈ [-1..1] | ||
textureMode is NORMAL, i.e., u, v texture coordinates ∈ [0..1] | ||
see: https://p5js.org/reference/#/p5/beginShape | ||
https://p5js.org/reference/#/p5/vertex | ||
y v | ||
| | | ||
(-1,1,0)| (1,1,0) (0,1) (1,1) | ||
*_____|_____* *__________* | ||
| | | | | | ||
|____NDC____|__x | texture | | ||
| | | | space | | ||
*_____|_____* *__________*___ u | ||
(-1,-1,0) (1,-1,0) (0,0) (1,0) | ||
*/ | ||
lumaShader.setUniform('texture', src); | ||
beginShape(); | ||
// format is: vertex(x, y, z, u, v) | ||
vertex(-1, -1, 0, 0, 1); | ||
vertex(1, -1, 0, 1, 1); | ||
vertex(1, 1, 0, 1, 0); | ||
vertex(-1, 1, 0, 0, 0); | ||
endShape(); | ||
} |
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