Exemple #1
0
        public void compile(WebGLRenderingContext webgl)
        {
            this.vs = webgl.CreateShader(webgl.VERTEX_SHADER);
            this.fs = webgl.CreateShader(webgl.FRAGMENT_SHADER);

            //分别编译shader
            webgl.ShaderSource(this.vs, this.vscode);
            webgl.CompileShader(this.vs);
            var r1 = webgl.GetShaderParameter(this.vs, webgl.COMPILE_STATUS);

            if (r1.As <bool>() == false)
            {
                alert(webgl.GetShaderInfoLog(this.vs));
            }
            //
            webgl.ShaderSource(this.fs, this.fscode);
            webgl.CompileShader(this.fs);
            var r2 = webgl.GetShaderParameter(this.fs, webgl.COMPILE_STATUS);

            if (r2.As <bool>() == false)
            {
                alert(webgl.GetShaderInfoLog(this.fs));
            }

            //program link
            this.program = webgl.CreateProgram().As <WebGLProgram>();

            webgl.AttachShader(this.program, this.vs);
            webgl.AttachShader(this.program, this.fs);

            webgl.LinkProgram(this.program);
            var r3 = webgl.GetProgramParameter(this.program, webgl.LINK_STATUS);

            if (r3.As <bool>() == false)
            {
                alert(webgl.GetProgramInfoLog(this.program));
            }


            //绑定vbo和shader顶点格式,这部分应该要区分材质改变与参数改变,可以少切换一些状态
            this.posPos    = webgl.GetAttribLocation(this.program, "position");
            this.posColor  = webgl.GetAttribLocation(this.program, "color");
            this.posColor2 = webgl.GetAttribLocation(this.program, "color2");

            this.posUV = webgl.GetAttribLocation(this.program, "uv");

            this.uniMatrix = webgl.GetUniformLocation(this.program, "matrix");
            this.uniTex0   = webgl.GetUniformLocation(this.program, "tex0");
            this.uniTex1   = webgl.GetUniformLocation(this.program, "tex1");
            this.uniCol0   = webgl.GetUniformLocation(this.program, "col0");
            this.uniCol1   = webgl.GetUniformLocation(this.program, "col1");
        }
Exemple #2
0
        public void InitShaders()
        {
            var fragmentShader = this.GetShader(gl, "shader-fs");
            var vertexShader   = this.GetShader(gl, "shader-vs");
            var shaderProgram  = gl.CreateProgram().As <WebGLProgram>();

            if (shaderProgram.Is <int>())
            {
                Global.Alert("Could not initialise program");
            }

            gl.AttachShader(shaderProgram, vertexShader);
            gl.AttachShader(shaderProgram, fragmentShader);
            gl.LinkProgram(shaderProgram);

            if (!gl.GetProgramParameter(shaderProgram, gl.LINK_STATUS).As <bool>())
            {
                Global.Alert("Could not initialise shaders");
            }

            gl.UseProgram(shaderProgram);

            this.vertexPositionAttribute = gl.GetAttribLocation(shaderProgram, "aVertexPosition");
            this.vertexNormalAttribute   = gl.GetAttribLocation(shaderProgram, "aVertexNormal");
            this.textureCoordAttribute   = gl.GetAttribLocation(shaderProgram, "aTextureCoord");

            gl.EnableVertexAttribArray(this.vertexPositionAttribute);
            gl.EnableVertexAttribArray(this.vertexNormalAttribute);
            gl.EnableVertexAttribArray(this.textureCoordAttribute);

            this.pMatrixUniform           = gl.GetUniformLocation(shaderProgram, "uPMatrix");
            this.mvMatrixUniform          = gl.GetUniformLocation(shaderProgram, "uMVMatrix");
            this.nMatrixUniform           = gl.GetUniformLocation(shaderProgram, "uNMatrix");
            this.samplerUniform           = gl.GetUniformLocation(shaderProgram, "uSampler");
            this.useLightingUniform       = gl.GetUniformLocation(shaderProgram, "uUseLighting");
            this.ambientColorUniform      = gl.GetUniformLocation(shaderProgram, "uAmbientColor");
            this.lightingDirectionUniform = gl.GetUniformLocation(shaderProgram, "uLightingDirection");
            this.directionalColorUniform  = gl.GetUniformLocation(shaderProgram, "uDirectionalColor");
            this.alphaUniform             = gl.GetUniformLocation(shaderProgram, "uAlpha");

            this.program = shaderProgram;
        }
Exemple #3
0
        WebGLProgram CreateShaderProgram(WebGLRenderingContext gl, WebGLShader vertShader, WebGLShader fragShader)
        {
            var prog = gl.CreateProgram().As <WebGLProgram>();

            gl.AttachShader(prog, vertShader);
            gl.AttachShader(prog, fragShader);
            gl.LinkProgram(prog);

            var status = gl.GetProgramParameter(prog, gl.LINK_STATUS);

            if (status.As <bool>())
            {
                return(prog);
            }

            var info = gl.GetProgramInfoLog(prog);

            gl.DeleteProgram(prog);

            throw new InvalidOperationException($"Unable to link program. Details: {info}");
        }
        private void InitShaders()
        {
            // create our shaders
            WebGLShader vertexShader   = LoadShader(GLES20.VERTEX_SHADER, @"
        attribute vec4 a_position;
        attribute vec4 a_color;
        attribute vec2 a_texCoord0;
        attribute vec2 a_texCoord1;
        uniform mat4 u_mvpMatrix;
        varying vec4 v_color;
        varying vec2 v_texCoord0;
        varying vec2 v_texCoord1;
        void main()
        {
            gl_Position = u_mvpMatrix * a_position;
            v_color = a_color;
            v_texCoord0 = a_texCoord0;
            v_texCoord1 = a_texCoord1;
        }");
            WebGLShader fragmentShader = LoadShader(GLES20.FRAGMENT_SHADER, @"
        #ifdef GL_ES
        precision mediump float;
        #endif
        uniform sampler2D s_texture0;
        uniform sampler2D s_texture1;
        uniform int s_texEnv0;
        uniform int s_texEnv1;
        uniform int u_enable_texture_0;
        uniform int u_enable_texture_1;
        varying vec4 v_color;
        varying vec2 v_texCoord0;
        varying vec2 v_texCoord1;
        vec4 finalColor;
        void main()
        {
            finalColor = v_color;
            if (u_enable_texture_0 == 1)
            {
                vec4 texel = texture2D(s_texture0, v_texCoord0);
                if (s_texEnv0 == 1) {
                    finalColor = finalColor * texel;
                } else if (s_texEnv0 == 2) {
                    finalColor = vec4(texel.r, texel.g, texel.b, finalColor.a);
                } else {
                    finalColor = texel;
                }
            }
            if (u_enable_texture_1 == 1) {
                vec4 texel = texture2D(s_texture1, v_texCoord1);
                if (s_texEnv1 == 1) {
                    finalColor = finalColor * texel;
                } else if (s_texEnv1 == 2) {
                    finalColor = vec4(texel.r, texel.g, texel.b, finalColor.a);
                } else {
                    finalColor = texel;
                }
            }
            // simple alpha check
            if (finalColor.a == 0.0) {
                discard;
            }
            float gamma = 1.5;
            float igamma = 1.0 / gamma;
            gl_FragColor = vec4(pow(finalColor.r, igamma), pow(finalColor.g, igamma), pow(finalColor.b, igamma), finalColor.a);
        }");

            if ((vertexShader == null) || (fragmentShader == null))
            {
                throw new Exception("RuntimeException: shader error");
            }
            // Create the program object
            WebGLProgram programObject = gl.CreateProgram();

            if ((programObject == null) || (gl.GetError() != GLES20.NO_ERROR))
            {
                throw new Exception("RuntimeException: program error");
            }
            // Attach our two shaders to the program
            gl.AttachShader(programObject, vertexShader);
            gl.AttachShader(programObject, fragmentShader);
            // Bind "vPosition" to attribute 0
            gl.BindAttribLocation(programObject, ARRAY_POSITION, "a_position");
            gl.BindAttribLocation(programObject, ARRAY_COLOR, "a_color");
            gl.BindAttribLocation(programObject, ARRAY_TEXCOORD_0, "a_texCoord0");
            gl.BindAttribLocation(programObject, ARRAY_TEXCOORD_1, "a_texCoord1");
            // Link the program
            gl.LinkProgram(programObject);
            // TODO(haustein) get position, color from the linker, too
            _uMvpMatrix      = gl.GetUniformLocation(programObject, "u_mvpMatrix");
            _uSampler0       = gl.GetUniformLocation(programObject, "s_texture0");
            _uSampler1       = gl.GetUniformLocation(programObject, "s_texture1");
            _uTexEnv0        = gl.GetUniformLocation(programObject, "s_texEnv0");
            _uTexEnv1        = gl.GetUniformLocation(programObject, "s_texEnv1");
            _uEnableTexture0 = gl.GetUniformLocation(programObject, "u_enable_texture_0");
            _uEnableTexture1 = gl.GetUniformLocation(programObject, "u_enable_texture_1");
            // Check the link status
            bool linked = (bool)gl.GetProgramParameter(programObject, GLES20.LINK_STATUS);

            if (!linked)
            {
                throw new Exception("RuntimeException: linker Error: " + gl.GetProgramInfoLog(programObject));
            }
            gl.UseProgram(programObject);
            gl.Uniform1i(_uSampler0, 0);
            gl.Uniform1i(_uSampler1, 1);
            gl.ActiveTexture(GLES20.TEXTURE0);
        }