'use strict';

window.OSG.globalify();

var osg = window.osg;
var osgUtil = window.osgUtil;

/*
    This filter makes the image look more sharp and defined.
    It is useful on blurred images to regain details
    It works by increasing the difference between near pixels
*/
function getPostSceneSharpen() {

    // Discrete Laplace convolution kernels from 
    // http://en.wikipedia.org/wiki/Laplace_filter#Implementation_in_Image_Processing
    function laplace( x ) {
        return [ 0, -x, 0, -x, x * 4, -x, 0, -x, 0 ];
    }

    function laplaceDiagonal( x ) {
        return [ -0.5 * x, -x, -0.5 * x, -x, x * 6, -x, -0.5 * x, -x, -0.5 * x ];
    }

    var inputTexture = osg.Texture.createFromURL( 'Medusa.png' );

    var kernel = osg.Uniform.createMatrix3( laplace( 1 ), 'kernel' );
    var useDiagonal = false;
    var factor = 0;

    function updateKernel() {

        if ( useDiagonal )
            kernel.set( laplace( factor ) );
        else
            kernel.set( laplaceDiagonal( factor ) );
    }

    // 3x3 tap, 9 textures fetches
    // For each texel, we sum the difference with its neighboors
    // and add this computed difference to the original texel
    // which produce a 'sharpened' look
    // TODO: decides if we keep the diagonal one
    // If yes, optimize the path witout diagonals
    var sharpenFilter = new osgUtil.Composer.Filter.Custom(
        [
            '#ifdef GL_ES',
            'precision highp float;',
            '#endif',

            'varying vec2 FragTexCoord0;',
            'uniform sampler2D inputTexture;',
            'uniform vec2 RenderSize;',
            'uniform mat3 kernel;',

            'float stepX = 1.0/RenderSize.x;',
            'float stepY = stepX;',

            'void main() {',

            'vec4 color = texture2D(inputTexture, FragTexCoord0);',

            'vec2 uv = FragTexCoord0;',
            'vec2 offset = vec2(0.0);',

            'for (int i=0; i < 3; i++)',
            '{',
            'offset.x = (1.0 - float(i)) * stepX;',
            'for (int j = 0; j < 3; j++)',
            '{',
            'offset.y = (1.0 - float(j)) * stepY;',

            'color += kernel[i][j] * texture2D(inputTexture, FragTexCoord0 + offset);',
            '}',
            '}',

            'gl_FragColor = color;',
            '}',
        ].join( '\n' ), {
            'kernel': kernel,
            'inputTexture': inputTexture
        }
    );

    var effect = {

        name: 'Sharpen',
        needCommonCube: false,

        buildComposer: function ( finalTexture ) {

            var composer = new osgUtil.Composer();
            composer.addPass( sharpenFilter, finalTexture );
            composer.build();

            return composer;
        },

        buildGui: function ( mainGui ) {

            var folder = mainGui.addFolder( this.name );
            folder.open();

            var kernel = {
                'kernel': 0,
                'sample diagonal': false
            };

            var kernelCtrl = folder.add( kernel, 'kernel', 0, 5.0 );
            var diagonalCtrl = folder.add( kernel, 'sample diagonal' );

            kernelCtrl.onChange( function ( value ) {
                factor = value;
                updateKernel();
            } );
            diagonalCtrl.onChange( function ( bool ) {
                useDiagonal = bool;
                updateKernel();
            } );
        }
    };

    return effect;
}
@@ -928,16 +928,24 @@
  laplace
+Diagonal
 ( factor
@@ -990,32 +990,24 @@
 set( laplace
-Diagonal
 ( factor ) )
@@ -1716,21 +1716,32 @@
 stepY = 
-stepX
+1.0/RenderSize.y
 ;',%0A%0A   
@@ -1780,16 +1780,17 @@
        '
+%09
 vec4 col
@@ -1846,24 +1846,25 @@
            '
+%09
 vec2 uv = Fr
@@ -1891,16 +1891,17 @@
        '
+%09
 vec2 off
@@ -1915,24 +1915,39 @@
 ec2(0.0);',%0A
+            '',
 %0A           
@@ -1940,32 +1940,33 @@
 ',%0A            '
+%09
 for (int i=0; i 
@@ -1974,32 +1974,17 @@
  3; i++)
-',%0A            '
+ 
 %7B',%0A    
@@ -1988,24 +1988,26 @@
            '
+%09%09
 offset.x = (
@@ -2046,16 +2046,18 @@
        '
+%09%09
 for (int
@@ -2058,19 +2058,17 @@
 r (int j
- = 
+=
 0; j %3C 3
@@ -2073,32 +2073,17 @@
  3; j++)
-',%0A            '
+ 
 %7B',%0A    
@@ -2091,16 +2091,19 @@
        '
+%09%09%09
 offset.y
@@ -2129,25 +2129,24 @@
  * stepY;',%0A
-%0A
             
@@ -2146,16 +2146,19 @@
        '
+%09%09%09
 color +=
@@ -2229,32 +2229,34 @@
 ',%0A            '
+%09%09
 %7D',%0A            
@@ -2252,24 +2252,25 @@
            '
+%09
 %7D',%0A%0A       
@@ -2275,16 +2275,17 @@
        '
+%09
 gl_FragC
@@ -3440,8 +3440,9 @@
 ffect;%0A%7D
+%0A

Fix the toggle of the diagonal kernel activation and code style