Пример #1
0
        public GdiImageTextureWithWhiteTransparentShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix; 
                varying vec2 v_texCoord;
                void main()
                {
                    gl_Position = u_mvpMatrix* a_position;
                    v_texCoord =  a_texCoord;
                 }	 
                ";
            //in fs, angle on windows
            //we need to switch color component
            //because we store value in memory as BGRA
            //and gl expect input in RGBA
            string fs = @"
                      precision mediump float;
                      varying vec2 v_texCoord;
                      uniform sampler2D s_texture;
                      void main()
                      {
                         vec4 c = texture2D(s_texture, v_texCoord); 
                         if((c[2] ==1.0) && (c[1]==1.0) && (c[0]== 1.0) && (c[3] == 1.0)){
                            discard;
                         }else{                                                   
                            gl_FragColor =  vec4(c[2],c[1],c[0],c[3]);  
                         }
                      }
                ";

            BuildProgram(vs, fs);
        }
Пример #2
0
        internal GLRenderSurface(int width, int height)
        {
            //-------------
            //y axis points upward (like other OpenGL)
            //x axis points to right.
            //please NOTE: left lower corner of the canvas is (0,0)
            //-------------

            this._width  = width;
            this._height = height;
            //setup viewport size,
            //we need W:H ratio= 1:1 , square viewport
            int max = Math.Max(width, height);

            orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //this make our viewport W:H =1:1

            flipVerticalView = MyMat4.scale(1, -1) * MyMat4.translate(new OpenTK.Vector3(0, -height, 0));
            orthoAndFlip     = orthoView * flipVerticalView;
            //-----------------------------------------------------------------------
            _shareRes           = new ShaderSharedResource();
            _shareRes.OrthoView = orthoView;
            //-----------------------------------------------------------------------
            basicFillShader     = new BasicFillShader(_shareRes);
            smoothLineShader    = new SmoothLineShader(_shareRes);
            rectFillShader      = new RectFillShader(_shareRes);
            gdiImgTextureShader = new GdiImageTextureShader(_shareRes);
            gdiImgTextureWithWhiteTransparentShader = new GdiImageTextureWithWhiteTransparentShader(_shareRes);
            glyphStencilShader        = new GlyphImageStecilShader(_shareRes);
            textureSubPixRendering    = new ImageTextureWithSubPixelRenderingShader(_shareRes);
            blurShader                = new BlurShader(_shareRes);
            glesTextureShader         = new OpenGLESTextureShader(_shareRes);
            invertAlphaFragmentShader = new InvertAlphaLineSmoothShader(_shareRes); //used with stencil  ***

            conv3x3TextureShader        = new Conv3x3TextureShader(_shareRes);
            msdfShader                  = new DrawingGL.MultiChannelSdf(_shareRes);
            msdfSubPixelRenderingShader = new DrawingGL.MultiChannelSubPixelRenderingSdf(_shareRes);
            sdfShader = new DrawingGL.SingleChannelSdf(_shareRes);
            //-----------------------------------------------------------------------
            //tools

            tessTool = new TessTool();
            //-----------------------------------------------------------------------


            //GL.Enable(EnableCap.CullFace);
            //GL.FrontFace(FrontFaceDirection.Cw);
            //GL.CullFace(CullFaceMode.Back);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);//original **

            //GL.BlendFunc(BlendingFactorSrc.SrcColor, BlendingFactorDest.One);// not apply alpha to src
            //GL.BlendFuncSeparate(BlendingFactorSrc.SrcColor, BlendingFactorDest.OneMinusSrcAlpha,
            //                     BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            //GL.BlendFuncSeparate(BlendingFactorSrc.SrcColor, BlendingFactorDest.OneMinusSrcColor, BlendingFactorSrc.SrcAlpha, BlendingFactorDest.Zero);

            GL.ClearColor(1, 1, 1, 1);
            //-------------------------------------------------------------------------------
            GL.Viewport(0, 0, width, height);
        }
Пример #3
0
        public OpenGLESTextureShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //--------------------------------------------------------------------------
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix; 
                varying vec2 v_texCoord;
                void main()
                {
                    gl_Position = u_mvpMatrix* a_position;
                    v_texCoord =  a_texCoord;
                 }	 
                ";
            //this case we not need to swap
            string fs = @"
                      precision mediump float;
                      varying vec2 v_texCoord;
                      uniform sampler2D s_texture;
                      void main()
                      {
                         gl_FragColor = texture2D(s_texture, v_texCoord);        
                      }
                ";

            BuildProgram(vs, fs);
        }
Пример #4
0
        public SingleChannelSdf(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //credit: https://www.mapbox.com/blog/text-signed-distance-fields/
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix;  
                uniform vec2 u_ortho_offset;
                varying vec2 v_texCoord;  
                void main()
                {
                    gl_Position = u_mvpMatrix* (a_position+vec4(u_ortho_offset,0,0));
                    v_texCoord =  a_texCoord; 
                 }	 
                ";
            string fs = @"
                precision mediump float;

                uniform sampler2D s_texture;
                uniform vec4 u_color;
                uniform float u_buffer;
                uniform float u_gamma;

                varying vec2 v_texCoord;

                void main() {
                    float dist = texture2D(s_texture, v_texCoord).r;
                    float alpha = smoothstep(u_buffer - u_gamma, u_buffer + u_gamma, dist);
                    gl_FragColor = vec4(u_color.rgb, alpha * u_color.a); 
                } 
             ";

            BuildProgram(vs, fs);
        }
Пример #5
0
        public RadialGradientFillShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;

            if (!LoadCompiledShader())
            {
                //vertex shader source
                string vs = @"        
                    attribute vec2 a_position;
                    uniform mat4 u_mvpMatrix;                     
 
                    void main()
                    {
                        gl_Position = u_mvpMatrix* vec4(a_position[0],a_position[1],0,1);  
                    }
                    ";
                //fragment source
                string fs = @"
                        precision mediump float; 
                        uniform vec3 u_center; 
                        uniform sampler2D s_texture;
                        uniform mat3 u_invertedTxMatrix;

                        void main()
                        {
                            vec4 pos=gl_FragCoord;                            
                            vec3 new_pos =  u_invertedTxMatrix* vec3(pos.x,pos.y,1.0);                            

                            //float r_distance= sqrt((pos.x-u_center.x)* (pos.x-u_center.x) + (pos.y -u_center.y)*(pos.y-u_center.y))/u_center.z;
                            float r_distance= sqrt((new_pos.x-u_center.x)* (new_pos.x-u_center.x) + (new_pos.y -u_center.y)*(new_pos.y-u_center.y))/(u_center.z);

                            if(r_distance >=0.9){
                               gl_FragColor= texture2D(s_texture,vec2(0.9,0.0));
                            }else{
                               gl_FragColor= texture2D(s_texture,vec2(r_distance,0.0));
                            }
                        }
                    ";

                //in fragment shader if we not 'clamp' r_distance
                //  if(r_distance > 1.0) r_distance =1
                //then the pattern will be repeat. ***

                if (!_shaderProgram.Build(vs, fs))
                {
                    throw new NotSupportedException();
                }

                SaveCompiledShader();
            }
            a_position         = _shaderProgram.GetAttrV2f("a_position");
            u_matrix           = _shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_center           = _shaderProgram.GetUniform3("u_center");
            s_texture          = _shaderProgram.GetUniform1("s_texture");
            u_invertedTxMatrix = _shaderProgram.GetUniformMat3("u_invertedTxMatrix");
        }
Пример #6
0
        public ShaderBase(ShaderSharedResource shareRes)
        {
            _shareRes      = shareRes;
            _shaderProgram = new MiniShaderProgram();

            EnableProgramBinaryCache = false; //force
            //EnableProgramBinaryCache = CachedBinaryShaderIO.HasBinCacheImpl;
        }
Пример #7
0
        public RectFillShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;

            if (!LoadCompiledShader())
            {
                //vertex shader source
                string vs = @"        
                    attribute vec2 a_position;     
                    attribute vec4 a_color;
                    uniform mat4 u_mvpMatrix; 
                    uniform vec2 u_ortho_offset;
                    
                    varying vec4 v_color;
 
                    void main()
                    {
                        gl_Position = u_mvpMatrix* vec4(a_position+ u_ortho_offset,0,1); 
                        v_color= a_color;
                    }
                    ";
                //fragment source
                string fs = @"
                        precision mediump float;
                        varying vec4 v_color; 
                        void main()
                        {
                            gl_FragColor = v_color;
                        }
                    ";
                if (!_shaderProgram.Build(vs, fs))
                {
                    throw new NotSupportedException();
                }

                SaveCompiledShader();
            }


            a_position     = _shaderProgram.GetAttrV2f("a_position");
            u_ortho_offset = _shaderProgram.GetUniform2("u_ortho_offset");
            a_color        = _shaderProgram.GetAttrV4f("a_color");
            u_matrix       = _shaderProgram.GetUniformMat4("u_mvpMatrix");
        }
Пример #8
0
        ShaderUniformVar4 _d_color; //drawing color
        public ImageTextureWithSubPixelRenderingShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix; 
                varying vec2 v_texCoord;
                void main()
                {
                    gl_Position = u_mvpMatrix* a_position;
                    v_texCoord =  a_texCoord;
                 }	 
                ";
            //in fs, angle on windows
            //we need to switch color component
            //because we store value in memory as BGRA
            //and gl expect input in RGBA
            string fs = @"
                      precision mediump float;

                      uniform sampler2D s_texture;
                      uniform int isBigEndian;
                      uniform int c_compo;
                      uniform vec4 d_color;
                      uniform float c_intensity;
  
                      varying vec2 v_texCoord; 
                      void main()
                      {                        
                         float v_texCoord0 =v_texCoord[0];
                         float v_texCoord1= v_texCoord[1]; 
                        

                         vec4 c= texture2D(s_texture,vec2(v_texCoord0 ,v_texCoord1));                          
                         if(c_compo==0){
                            gl_FragColor = vec4(d_color[0],d_color[1],d_color[2],(c[0]* d_color[3])*c_intensity);
                         }else if(c_compo==1){
                            gl_FragColor = vec4(d_color[0],d_color[1],d_color[2],(c[1]* d_color[3])*c_intensity);
                         }else{
                            gl_FragColor = vec4(d_color[0],d_color[1],d_color[2],(c[2]* d_color[3])*c_intensity);
                         }
                      }
                ";

            BuildProgram(vs, fs);
        }
Пример #9
0
        public MultiChannelSdf(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //credit: https://github.com/Chlumsky/msdfgen

            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix;  
                varying vec2 v_texCoord;  
                void main()
                {
                    gl_Position = u_mvpMatrix* a_position;
                    v_texCoord =  a_texCoord; 
                 }	 
                ";
            //enable derivative extension  for fwidth() function
            //see
            //https://www.khronos.org/registry/gles/extensions/OES/OES_standard_derivatives.txt
            //https://github.com/AnalyticalGraphicsInc/cesium/issues/745
            //https://developer.mozilla.org/en-US/docs/Web/API/OES_standard_derivatives
            //https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Using_Extensions
            string fs = @"
                        #ifdef GL_OES_standard_derivatives
                            #extension GL_OES_standard_derivatives : enable
                        #endif  
                        precision mediump float; 
                        varying vec2 v_texCoord;                
                        uniform sampler2D s_texture; //msdf texture 
                        uniform vec4 fgColor;

                        float median(float r, float g, float b) {
                            return max(min(r, g), min(max(r, g), b));
                        }
                        void main() {
                            vec4 sample = texture2D(s_texture, v_texCoord);
                            float sigDist = median(sample[0], sample[1], sample[2]) - 0.5;
                            float opacity = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0); 
                            vec4 finalColor=vec4(fgColor[0],fgColor[1],fgColor[2],opacity);
                            //mix(bgColor, fgColor, opacity);  
                            gl_FragColor= finalColor;
                        }
             ";

            BuildProgram(vs, fs);
        }
Пример #10
0
        ShaderUniformVar1 u_alpha;//** alpha component to apply with original img
        public BGRImageTextureShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            Alpha = 1f;//default
            //--------------------------------------------------------------------------
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform vec2 u_ortho_offset;
                uniform mat4 u_mvpMatrix;

                varying vec2 v_texCoord;
                void main()
                {
                    gl_Position = u_mvpMatrix* (a_position+ vec4(u_ortho_offset,0,0));
                    v_texCoord =  a_texCoord;
                 }	 
                ";
            //in fs, angle on windows
            //we need to switch color component ***
            //because we store value in memory as BGRA
            //and gl expect input in RGBA
            string fs = @"
                      precision mediump float;
                      varying vec2 v_texCoord;
                      uniform sampler2D s_texture;
                      uniform float u_alpha;
                      void main()
                      {
                         vec4 c = texture2D(s_texture, v_texCoord);                            
                         gl_FragColor = vec4(c[2],c[1],c[0],u_alpha);
                      }
                ";

            BuildProgram(vs, fs);
        }
Пример #11
0
        public RectFillShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //----------------
            //vertex shader source
            string vs = @"        
            attribute vec2 a_position;     
            attribute vec4 a_color;
            uniform mat4 u_mvpMatrix; 
            varying vec4 v_color;
 
            void main()
            {
                gl_Position = u_mvpMatrix* vec4(a_position[0],a_position[1],0,1); 
                v_color= a_color;
            }
            ";
            //fragment source
            string fs = @"
                precision mediump float;
                varying vec4 v_color; 
                void main()
                {
                    gl_FragColor = v_color;
                }
            ";

            if (!shaderProgram.Build(vs, fs))
            {
                throw new NotSupportedException();
            }

            a_position = shaderProgram.GetAttrV2f("a_position");
            a_color    = shaderProgram.GetAttrV4f("a_color");
            u_matrix   = shaderProgram.GetUniformMat4("u_mvpMatrix");
        }
Пример #12
0
 public BlurShader(ShaderSharedResource shareRes)
     : base(shareRes)
 {
     BuildShaderV3();
 }
Пример #13
0
        public Conv3x3TextureShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //--------------------------------------------------------------------------
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform mat4 u_mvpMatrix;  
                varying vec2 v_texCoord;  
                void main()
                {
                    gl_Position = u_mvpMatrix* a_position;
                    v_texCoord =  a_texCoord; 
                 }	 
                ";
            //in fs, angle on windows
            //we need to switch color component
            //because we store value in memory as BGRA
            //and gl expect input in RGBA
            string fs = @"
                      precision mediump float;
                     
                      uniform sampler2D s_texture;
                      uniform int isBigEndian;
                      uniform mat3 convKernel;
                      uniform float kernelWeight;  
                      uniform vec2 onepix_xy;
                        
                      varying vec2 v_texCoord; 
                      void main()
                      {                         
                        vec4 c = vec4(0.0);
                        float v_texCoord1= v_texCoord[1];
                        float v_texCoord0 =v_texCoord[0];
                        float one_x=onepix_xy[0];               
                        float one_y=onepix_xy[1];

                        c += texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1+one_y))*convKernel[0][0];
                        c += texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1+one_y))*convKernel[0][1];
                        c += texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1+one_y))*convKernel[0][2];

                        c += texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1))*convKernel[1][0];
                        c += texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1))*convKernel[1][1];
                        c += texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1))*convKernel[1][2];

                        c += texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1-one_y))*convKernel[2][0];
                        c += texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1-one_y))*convKernel[2][1];
                        c += texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1-one_y))*convKernel[2][2];
                         
                        c= c / kernelWeight;

                        if(isBigEndian ==1){
                            gl_FragColor = vec4(c[0],c[1],c[2],1);
                        }else{
                            gl_FragColor = vec4(c[2],c[1],c[0],1);
                        }
                      }
                ";

            BuildProgram(vs, fs);
            SetConvolutionKernel(Mat3x3ConvGen.gaussianBlur);
        }
Пример #14
0
 public SimpleRectTextureShader(ShaderSharedResource shareRes)
     : base(shareRes)
 {
 }
        public InvertAlphaLineSmoothShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //-------------------------------------------------------------------------------
            string vs = @"                   
            attribute vec4 a_position;    

            uniform mat4 u_mvpMatrix;
            uniform vec4 u_solidColor; 
            uniform float u_linewidth;

            varying vec4 v_color; 
            varying float v_distance;
            varying float p0;
            
            void main()
            {   
                
                float rad = a_position[3];
                v_distance= a_position[2];

                float n_x = sin(rad); 
                float n_y = cos(rad);  

                vec4 delta;
                if(v_distance <1.0){                                         
                    delta = vec4(-n_x * u_linewidth,n_y * u_linewidth,0,0);                       
                }else{                      
                    delta = vec4(n_x * u_linewidth,-n_y * u_linewidth,0,0);
                }
    
                if(u_linewidth <= 0.5){
                    p0 = 0.5;      
                }else if(u_linewidth <=1.0){
                    p0 = 0.45;  
                }else if(u_linewidth>1.0 && u_linewidth<3.0){
                    
                    p0 = 0.25;  
                }else{
                    p0= 0.1;
                }
                
                vec4 pos = vec4(a_position[0],a_position[1],0,1) + delta;                 
                gl_Position = u_mvpMatrix* pos;                

                v_color= u_solidColor;
            }
            ";
            //fragment source
            //this is invert fragment shader ***
            //so we
            string fs = @"
                precision mediump float;
                varying vec4 v_color;  
                varying float v_distance;
                varying float p0;                
                void main()
                {
                    float d0= v_distance; 
                    float p1= 1.0-p0;
                    float factor= 1.0 /p0;
            
                    if(d0 < p0){                        
                        gl_FragColor = vec4(v_color[0],v_color[1],v_color[2], 1.0 -(v_color[3] *(d0 * factor)));
                    }else if(d0> p1){                         
                        gl_FragColor= vec4(v_color[0],v_color[1],v_color[2],1.0-(v_color[3] *((1.0-d0)* factor)));
                    }
                    else{ 
                       gl_FragColor = vec4(0,0,0,0);                        
                    } 
                }
            ";

            //---------------------
            if (!shaderProgram.Build(vs, fs))
            {
                return;
            }
            //-----------------------

            a_position   = shaderProgram.GetAttrV4f("a_position");
            u_matrix     = shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_solidColor = shaderProgram.GetUniform4("u_solidColor");
            u_linewidth  = shaderProgram.GetUniform1("u_linewidth");
            _strokeColor = Drawing.Color.Black;
        }
Пример #16
0
 public ColorFillShaderBase(ShaderSharedResource shareRes)
     : base(shareRes)
 {
 }
Пример #17
0
 public MultiChannelSubPixelRenderingSdf(ShaderSharedResource shareRes)
     : base(shareRes)
 {
     BuildProgramV1();
     //BuildProgramV2();
 }
        public InvertAlphaLineSmoothShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;


            //TODO: review here ...
            if (!LoadCompiledShader())
            {
                //vertex shader source
                string vs = @"                   
                attribute vec4 a_position;    

                uniform vec2 u_ortho_offset; 
                uniform mat4 u_mvpMatrix;
                uniform vec4 u_solidColor; 
                uniform float u_linewidth;

                varying vec4 v_color; 
                varying float v_distance;
                varying float p0;
            
                void main()
                {   
                
                    float rad = a_position[3];
                    v_distance= a_position[2];

                    float n_x = sin(rad); 
                    float n_y = cos(rad);  

                    vec2 delta;
                    if(v_distance <1.0){                                         
                        delta = vec2(-n_x * u_linewidth,n_y * u_linewidth) +u_ortho_offset;                       
                    }else{                      
                        delta = vec2(n_x * u_linewidth,-n_y * u_linewidth)+ u_ortho_offset;
                    }
    
                    if(u_linewidth <= 0.5){
                        p0 = 0.5;      
                    }else if(u_linewidth <=1.0){
                        p0 = 0.45;  
                    }else if(u_linewidth>1.0 && u_linewidth<3.0){
                    
                        p0 = 0.25;  
                    }else{
                        p0= 0.1;
                    } 
                     
                    gl_Position = u_mvpMatrix*  vec4(a_position[0] +delta[0],a_position[1]+delta[1],0,1);
                    v_color= u_solidColor;
                }
                ";
                //fragment source
                //this is invert fragment shader ***
                //so we
                string fs = @"
                    precision mediump float;
                    varying vec4 v_color;  
                    varying float v_distance;
                    varying float p0;                
                    void main()
                    {
                        float d0= v_distance; 
                        float p1= 1.0-p0;
                        float factor= 1.0 /p0;
            
                        if(d0 < p0){                        
                            gl_FragColor = vec4(v_color[0],v_color[1],v_color[2], 1.0 -(v_color[3] *(d0 * factor)));
                        }else if(d0> p1){                         
                            gl_FragColor= vec4(v_color[0],v_color[1],v_color[2],1.0-(v_color[3] *((1.0-d0)* factor)));
                        }
                        else{ 
                           gl_FragColor = vec4(0,0,0,0);                        
                        } 
                    }
                ";
                //---------------------
                if (!_shaderProgram.Build(vs, fs))
                {
                    return;
                }

                //-----------------------
                SaveCompiledShader();
            }


            a_position      = _shaderProgram.GetAttrV4f("a_position");
            u_orthov_offset = _shaderProgram.GetUniform2("u_ortho_offset");
            u_matrix        = _shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_solidColor    = _shaderProgram.GetUniform4("u_solidColor");
            u_linewidth     = _shaderProgram.GetUniform1("u_linewidth");
            _strokeColor    = Drawing.Color.Black;
        }
Пример #19
0
        public Conv3x3TextureShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //--------------------------------------------------------------------------
            string vs = @"
                attribute vec4 a_position;
                attribute vec2 a_texCoord;
                uniform vec2 u_ortho_offset;
                uniform mat4 u_mvpMatrix;  
                varying vec2 v_texCoord;  
                void main()
                {
                    gl_Position = u_mvpMatrix* (a_position +vec4(u_ortho_offset,0,0));
                    v_texCoord =  a_texCoord; 
                 }	 
                ";
            //in fs, angle on windows
            //we need to switch color component
            //because we store value in memory as BGRA
            //and gl expect input in RGBA
            string fs = @"
                      precision mediump float;
                     
                      uniform sampler2D s_texture; 
                      uniform mat3 convKernel;
                      uniform float kernelWeight;  
                      uniform vec2 onepix_xy;
                        
                      varying vec2 v_texCoord; 
                      void main()
                      {                         
                        
                        float v_texCoord1= v_texCoord[1];
                        float v_texCoord0 =v_texCoord[0];
                        float one_x=onepix_xy[0];               
                        float one_y=onepix_xy[1];

                        gl_FragColor = ( texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1+one_y))*convKernel[0][0]+
                                         texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1+one_y))*convKernel[0][1]+
                                         texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1+one_y))*convKernel[0][2]+

                                         texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1))*convKernel[1][0]+
                                         texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1))*convKernel[1][1]+
                                         texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1))*convKernel[1][2]+

                                         texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1-one_y))*convKernel[2][0]+
                                         texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1-one_y))*convKernel[2][1]+
                                         texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1-one_y))*convKernel[2][2]) / kernelWeight; 
                      }
                ";

            //string fs = @"
            //          precision mediump float;

            //          uniform sampler2D s_texture;
            //          uniform int isBigEndian;
            //          uniform mat3 convKernel;
            //          uniform float kernelWeight;
            //          uniform vec2 onepix_xy;

            //          varying vec2 v_texCoord;
            //          void main()
            //          {

            //            float v_texCoord1= v_texCoord[1];
            //            float v_texCoord0 =v_texCoord[0];
            //            float one_x=onepix_xy[0];
            //            float one_y=onepix_xy[1];

            //            vec4 c = (texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1+one_y))*convKernel[0][0]+
            //                     texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1+one_y))*convKernel[0][1]+
            //                     texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1+one_y))*convKernel[0][2]+

            //                     texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1))*convKernel[1][0]+
            //                     texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1))*convKernel[1][1]+
            //                     texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1))*convKernel[1][2]+

            //                     texture2D(s_texture,vec2(v_texCoord0-one_x,v_texCoord1-one_y))*convKernel[2][0]+
            //                     texture2D(s_texture,vec2(v_texCoord0      ,v_texCoord1-one_y))*convKernel[2][1]+
            //                     texture2D(s_texture,vec2(v_texCoord0+one_x,v_texCoord1-one_y))*convKernel[2][2]) / kernelWeight;

            //            if(isBigEndian ==1){
            //                gl_FragColor = vec4(c[0],c[1],c[2],1);
            //            }else{
            //                gl_FragColor = vec4(c[2],c[1],c[0],1);
            //            }
            //          }
            //    ";
            BuildProgram(vs, fs);
            SetConvolutionKernel(Mat3x3ConvGen.gaussianBlur);
            _imgSizeChanged = true;
        }
Пример #20
0
        public SmoothLineShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;

            if (!LoadCompiledShader())
            {
                //we may store this outside the exe ?

                //vertex shader source

                string vs = @"        
                    precision mediump float;
                    attribute vec4 a_position;  
                    uniform vec2 u_ortho_offset; 
                     
                    uniform mat4 u_mvpMatrix; 
                    uniform float u_linewidth;                  
                    varying float v_distance; 
                    varying vec2 v_dir; 
                    void main()
                    {                   
                        float rad = a_position[3];
                        v_distance= a_position[2]; 
                        vec2 delta;
                        if(v_distance <1.0){                                         
                            delta = vec2(-sin(rad) * u_linewidth,cos(rad) * u_linewidth) + u_ortho_offset;                       
                            v_dir = vec2(0.80,0.0);
                        }else{                      
                            delta = vec2(sin(rad) * u_linewidth,-cos(rad) * u_linewidth) + u_ortho_offset;
                            v_dir = vec2(0.0,0.80);  
                        } 
                        gl_Position = u_mvpMatrix*  vec4(a_position[0] +delta[0],a_position[1]+delta[1],0,1);
                    }
                ";



                //version3
                string fs = @"
                    precision mediump float;
                    uniform vec4 u_solidColor;
                    uniform float p0;
                    varying float v_distance;
                    varying vec2 v_dir;                      
                    void main()
                    {   
                         gl_FragColor =vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], 
                                            u_solidColor[3] *((v_distance* (v_dir[0])+ (1.0-v_distance)* (v_dir[1]))  * (1.0/p0)) * 0.55);  
                    }
                ";

                ////old version 2
                //string fs = @"
                //    precision mediump float;
                //    uniform vec4 u_solidColor;
                //    uniform float p0;
                //    varying float v_distance;
                //    void main()
                //    {
                //        if(v_distance < p0){
                //            gl_FragColor =vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], u_solidColor[3] *(v_distance * (1.0/p0)) * 0.55);
                //        }else{
                //            gl_FragColor =vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], u_solidColor[3] *((1.0-v_distance) * (1.0/p0)) * 0.55);
                //        }
                //    }
                //";
                ////old version 1
                //string fs = @"
                //    precision mediump float;
                //    uniform vec4 u_solidColor;
                //    uniform float p0;
                //    varying float v_distance;
                //    void main()
                //    {

                //        if(v_distance < p0){
                //            gl_FragColor =vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], u_solidColor[3] *(v_distance * (1.0/p0)) * 0.55);
                //        }else if(v_distance >= (1.0-p0)){
                //            gl_FragColor =vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], u_solidColor[3] *((1.0-v_distance) * (1.0/p0)) * 0.55);
                //        }else{
                //            gl_FragColor = u_solidColor;
                //        }
                //    }
                //";


                //---------------------
                if (!_shaderProgram.Build(vs, fs))
                {
                    return;
                }
                //
                SaveCompiledShader();
            }


            //-----------------------
            a_position     = _shaderProgram.GetAttrV4f("a_position");
            u_ortho_offset = _shaderProgram.GetUniform2("u_ortho_offset");
            u_matrix       = _shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_solidColor   = _shaderProgram.GetUniform4("u_solidColor");
            u_linewidth    = _shaderProgram.GetUniform1("u_linewidth");
            u_p0           = _shaderProgram.GetUniform1("p0");
        }
Пример #21
0
        public InvertAlphaLineSmoothShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;

            if (!LoadCompiledShader())
            {
                //vertex shader source
                string vs = @"                   
                attribute vec4 a_position;     
                uniform mat4 u_mvpMatrix; 
                uniform float u_linewidth;
                uniform vec2 u_ortho_offset;  
                varying float v_distance; 
                void main()
                {                   
                    float rad = a_position[3];
                    v_distance= a_position[2]; 

                    vec2 delta;
                    if(v_distance <1.0){                                         
                        delta = vec2(-sin(rad) * u_linewidth,cos(rad) * u_linewidth) + u_ortho_offset;   
                    }else{                      
                        delta = vec2(sin(rad) * u_linewidth,-cos(rad) * u_linewidth)+ u_ortho_offset;  
                    } 
                    gl_Position = u_mvpMatrix*  vec4(a_position[0] +delta[0],a_position[1]+delta[1],0,1); 
                }
                ";
                //fragment source
                //this is invert fragment shader ***


                //p0= cutpoint of inside,outside
                //1/p0 = factor
                string fs = @"
                    precision mediump float;      
                    uniform float p0; 
                    varying float v_distance; 
                    void main()
                    {  
                        if(v_distance < p0){                        
                            gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0-((v_distance * (1.0 /p0))));
                        }else if(v_distance> (1.0-p0)){                         
                            gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0-(((1.0-v_distance)* (1.0 /p0))));
                        }
                        else{ 
                            discard;                      
                        } 
                    }
                ";


                //string fs = @"
                //    precision mediump float;
                //    uniform vec4 u_solidColor;
                //    uniform float p0;
                //    varying vec2 v_dir;
                //    varying float v_distance;

                //    void main()
                //    {
                //        float factor= 1.0 /p0;
                //        if(v_distance < p0){
                //            gl_FragColor = vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], 1.0-(u_solidColor[3] *(v_distance * factor)));
                //        }else if(v_distance> (1.0-p0)){
                //            gl_FragColor = vec4(u_solidColor[0],u_solidColor[1],u_solidColor[2], 1.0-(u_solidColor[3] *((1.0-v_distance)* factor)));
                //        }
                //        else{
                //            discard;
                //        }
                //    }
                //";

                //---------------------
                if (!_shaderProgram.Build(vs, fs))
                {
                    return;
                }

                //-----------------------
                SaveCompiledShader();
            }


            a_position     = _shaderProgram.GetAttrV4f("a_position");
            u_matrix       = _shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_ortho_offset = _shaderProgram.GetUniform2("u_ortho_offset");
            u_linewidth    = _shaderProgram.GetUniform1("u_linewidth");
            u_p0           = _shaderProgram.GetUniform1("p0");
            _cutPoint      = SetCutPoint(0.5f); //this are fixed for inverted alpha smooth line shader
        }
Пример #22
0
 public ShaderBase(ShaderSharedResource shareRes)
 {
     _shareRes = shareRes;
 }
Пример #23
0
        public SmoothLineShader(ShaderSharedResource shareRes)
            : base(shareRes)
        {
            //NOTE: during development,
            //new shader source may not recompile if you don't clear cache or disable cache feature
            //like...
            //EnableProgramBinaryCache = false;

            if (!LoadCompiledShader())
            {
                //we may store this outside the exe ?

                //vertex shader source
                string vs = @"                   
                    attribute vec4 a_position;  
                    uniform mat4 u_mvpMatrix;
                    uniform vec4 u_solidColor;
                
                    uniform float u_linewidth;
                    varying vec4 v_color; 
                    varying float v_distance;
                    varying float p0;
            
                    void main()
                    {   
                
                        float rad = a_position[3];
                        v_distance= a_position[2];
                        float n_x = sin(rad); 
                        float n_y = cos(rad);  
                        vec4 delta;
                        if(v_distance <1.0){                                         
                            delta = vec4(-n_x * u_linewidth,n_y * u_linewidth,0,0);                       
                        }else{                      
                            delta = vec4(n_x * u_linewidth,-n_y * u_linewidth,0,0);
                        }
    
                         if(u_linewidth <= 0.5){
                            p0 = 0.5;      
                        }else if(u_linewidth <=1.0){
                            p0 = 0.475;  
                        }else if(u_linewidth>1.0 && u_linewidth<3.0){                    
                            p0 = 0.25;  
                        }else{
                            p0= 0.1;
                        }
                
                        vec4 pos = vec4(a_position[0],a_position[1],0,1) + delta;                 
                        gl_Position = u_mvpMatrix* pos;                
                        v_color= u_solidColor;
                    }
                ";

                //fragment source
                //float factor= 1.0 /p0;
                string fs = @"
                    precision mediump float;
                    varying vec4 v_color;  
                    varying float v_distance;
                    varying float p0;                
                    void main()
                    {    
                        if(v_distance < p0){                        
                            gl_FragColor =vec4(v_color[0],v_color[1],v_color[2], v_color[3] *(v_distance * (1.0/p0)) * 0.55);
                        }else if(v_distance > (1.0-p0)){                         
                            gl_FragColor =vec4(v_color[0],v_color[1],v_color[2], v_color[3] *((1.0-v_distance) * (1.0/p0) * 0.55));
                        }
                        else{ 
                            gl_FragColor =v_color; 
                        } 
                    }
                ";
                //---------------------
                if (!_shaderProgram.Build(vs, fs))
                {
                    return;
                }
                //
                SaveCompiledShader();
            }


            //-----------------------
            a_position   = _shaderProgram.GetAttrV4f("a_position");
            u_matrix     = _shaderProgram.GetUniformMat4("u_mvpMatrix");
            u_solidColor = _shaderProgram.GetUniform4("u_solidColor");
            u_linewidth  = _shaderProgram.GetUniform1("u_linewidth");
        }