Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distorsion effect contribution #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Full documentation is in progress at the [wiki](https:/brianchirls/S
- Directional Blur
- Displacement Map
- Dither
- Distortion
- Edge Detect
- Emboss
- Exposure Adjust
Expand Down Expand Up @@ -115,4 +116,4 @@ Individual plugins may be licensed differently. Check source code comments.

## Credits

Seriously.js is created and maintained by [Brian Chirls](http://chirls.com)
Seriously.js is created and maintained by [Brian Chirls](http://chirls.com)
101 changes: 101 additions & 0 deletions effects/seriously.distortion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
(function (root, factory) {
'use strict';

if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['seriously'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('seriously'));
} else {
if (!root.Seriously) {
root.Seriously = { plugin: function (name, opt) { this[name] = opt; } };
}
factory(root.Seriously);
}
}(window, function (Seriously) {
'use strict';

Seriously.plugin('distortion', {
commonShader: true,
shader: function (inputs, shaderSource) {
shaderSource.vertex = [

'precision highp float;',


'attribute vec3 aVertexPosition;',

'attribute vec2 aTextureCoord;',

'varying vec3 vPosition;',
'varying vec2 vTextureCoord;',

'void main(void){',
'vPosition = aVertexPosition;',
'vTextureCoord = aTextureCoord;',

'gl_Position = vec4(vPosition,1.0);',
'}',
].join('\n');
shaderSource.fragment = [
'precision highp float;',

'uniform vec3 uLensS;',
'uniform vec2 uLensF;',

'uniform vec2 uFov;',

'uniform sampler2D source;',

'varying vec3 vPosition;',
'varying vec2 vTextureCoord;',

'vec2 GLCoord2TextureCoord(vec2 glCoord) {',
' return glCoord * vec2(1.0, -1.0)/ 2.0 + vec2(0.5, 0.5);',
'}',

'void main(void){',
'float scale = uLensS.z;',
'vec3 vPos = vPosition;',
'float Fx = uLensF.x;',
'float Fy = uLensF.y;',
'vec2 vMapping = vPos.xy;',
'vMapping.x = vMapping.x + ((pow(vPos.y, 2.0)/scale)*vPos.x/scale)*-Fx;',
'vMapping.y = vMapping.y + ((pow(vPos.x, 2.0)/scale)*vPos.y/scale)*-Fy;',
'vMapping = vMapping * uLensS.xy;',
'vMapping = GLCoord2TextureCoord(vMapping/scale);',
'vec4 texture = texture2D(source, vec2(vMapping.x, 1.0 - abs(vMapping.y)));',
'if(vMapping.x > 0.99 || vMapping.x < 0.01 || vMapping.y > 0.99 || vMapping.y < 0.01){',
' texture = vec4(0.0, 0.0, 0.0, 1.0);',
'}',
'gl_FragColor = texture;',
'}'
].join('\n');
return shaderSource;
},
inPlace: true,
inputs: {
source: {
type: 'image',
uniform: 'source'
},
uLensS: {
type: 'vector',
uniform: 'uLensS',
dimensions: 3,
defaultValue: [1.0, 1.0, 1.5]
},
uLensF: {
type: 'vector',
uniform: 'uLensF',
dimensions: 2,
defaultValue: [0.0, 0.0]
}


},
title: 'distortion',
description: ''
});
}));