LoginSignup
2
2

More than 5 years have passed since last update.

WebGLのuniformで構造体の配列をシェーダーに渡す方法

Last updated at Posted at 2018-09-30

WebGLのシェーダー上で構造体の配列として宣言されたuniformに値を渡す方法です。
JavaScriptから配列のインデックスと構造体のプロパティを一つずつ指定して値を指定していきます。

sample.js

gl.uniform3f(gl.getUniformLocation(program, 'u_mystruct[0].color1'), 1, 0, 0);
gl.uniform3f(gl.getUniformLocation(program, 'u_mystruct[0].color2'), 0, 1, 0);
gl.uniform3f(gl.getUniformLocation(program, 'u_mystruct[1].color1'), 0, 0, 1);
gl.uniform3f(gl.getUniformLocation(program, 'u_mystruct[1].color2'), 0, 0, 0);
sample.glsl
struct MyStruct {
    vec3 color1;
    vec3 color2;
};

uniform MyStruct u_mystruct[2];

void main(void) {
    gl_FragColor = vec4(u_mystruct[0].color1 + u_mystruct[0].color2 + u_mystruct[1].color1+ u_mystruct[1].color2, 1.0);
}

 参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2