From 9922846acc033b597859d1aa615460aa1572fa44 Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 28 Sep 2021 15:27:00 +0900 Subject: [PATCH] shader uniform --- index.ts | 14 +++++++++----- src/fragment.frag | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/index.ts b/index.ts index 8793d81..0505bac 100644 --- a/index.ts +++ b/index.ts @@ -27,8 +27,12 @@ class Renderer{ gl : WebGL2RenderingContextStrict; constructor(gl: WebGL2RenderingContextStrict){ this.gl = gl; + } + init(){ + const gl = this.gl; + let program: WebGLProgram; try{ - const program = createProgramFromSource(gl,vert_src,frag_src); + program = createProgramFromSource(gl,vert_src,frag_src); gl.useProgram(program); } catch(e){ @@ -40,9 +44,6 @@ class Renderer{ } else throw e; } - } - init(){ - const gl = this.gl; const position = [ -0.5,-0.5, 0.5,-0.5, @@ -60,7 +61,10 @@ class Renderer{ const indexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(index),gl.STATIC_DRAW); - + + let location = gl.getUniformLocation(program, "u_color"); //u_color 변수 위치를 참조 + gl.uniform4f(location, 0.8, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달 + } draw(){ const gl = this.gl; diff --git a/src/fragment.frag b/src/fragment.frag index 410c252..67ca4a9 100644 --- a/src/fragment.frag +++ b/src/fragment.frag @@ -1,7 +1,10 @@ #version 300 es precision highp float; layout(location=0) out vec4 outColor; +//in vec4 gl_FragCoord; +//in vec2 gl_PointCoord; +uniform vec4 u_color; void main() { - outColor = vec4(0.0,0.0,1.0,1.0); + outColor = u_color; } \ No newline at end of file