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"); }
public WebGLShader GetShader(WebGLRenderingContext gl, string id) { var shaderScript = Document.GetElementById(id).As <HTMLScriptElement>(); if (shaderScript == null) { return(null); } var str = ""; var k = shaderScript.FirstChild; while (k != null) { if (k.NodeType == NodeType.Text) { str += k.TextContent; } k = k.NextSibling; } WebGLShader shader; if (shaderScript.Type == "x-shader/x-fragment") { shader = gl.CreateShader(gl.FRAGMENT_SHADER); } else if (shaderScript.Type == "x-shader/x-vertex") { shader = gl.CreateShader(gl.VERTEX_SHADER); } else { return(null); } gl.ShaderSource(shader, str); gl.CompileShader(shader); if (!gl.GetShaderParameter(shader, gl.COMPILE_STATUS).As <bool>()) { Global.Alert(gl.GetShaderInfoLog(shader)); return(null); } return(shader); }
WebGLShader CreateShader(WebGLRenderingContext gl, int type, string source) { var shader = gl.CreateShader(type); gl.ShaderSource(shader, source); gl.CompileShader(shader); var status = gl.GetShaderParameter(shader, gl.COMPILE_STATUS); if (status.As <bool>()) { return(shader); } var info = gl.GetShaderInfoLog(shader); gl.DeleteShader(shader); throw new InvalidOperationException($"Unable to compile shader. Details: {info}"); }
private WebGLShader LoadShader(uint shaderType, string shaderSource) { // Create the shader object WebGLShader shader = gl.CreateShader(shaderType); if (shader == null) { throw new Exception("RuntimeException:"); } // Load the shader source gl.ShaderSource(shader, shaderSource); // Compile the shader gl.CompileShader(shader); // Check the compile status bool compiled = (bool)gl.GetShaderParameter(shader, GLES20.COMPILE_STATUS); if (!compiled) { // Something went wrong during compilation; get the error throw new Exception("RuntimeException: Shader compile error: " + gl.GetShaderInfoLog(shader)); } return(shader); }