尝试在for循环内递增变量时发生glls错误

时间:2019-03-07 14:04:51

标签: glsl regl

上下文:我将一个统一的float数组传递给片段着色器,然后使用该数组中的值初始化一个float,最后尝试使用以下代码在for循环中递增该float:

var regl = createREGL()

var drawTriangle = regl({
  vert: `
  precision mediump float;
  attribute vec2 position;

  void main () {
    gl_Position = vec4(position, 0, 1);
  }
  `,

  frag: `
  precision mediump float;
  uniform float refPoints[3];
  varying vec3 fcolor;

  void main () {
    float a_value = refPoints[1];
    for(int i=0;i<3;++i) {
      a_value += 1.; // <-- this line is causing the error
    }
    gl_FragColor = vec4(1, 1, 1, 1);
  }
  `,
  attributes: {
    position: [
      [1, 0],
      [0, 1],
      [-1, -1]
    ],
  },
  uniforms : {
    refPoints : [0., 1., 0., 2.]
  },
  count: 3
})

regl.frame(function () {
  regl.clear({
    color: [0, 0, 0, 1],
    depth: 1
  })
  drawTriangle()
})
<script src="https://npmcdn.com/regl/dist/regl.js"></script>

我有一个奇怪的错误:

Error: (regl) bad data or missing for uniform "refPoints[0]".  invalid type, expected number in command unknown

我不明白为什么这个特定循环中的这种增量会引起很多麻烦。你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

AFAICT这是regl中的错误吗?如果您使用

    uniforms: { 
      "refPoints[0]": 0.,
      "refPoints[1]": 1.,
      "refPoints[2]": 0.,
    }

var regl = createREGL()

var drawTriangle = regl({
  vert: `
  precision mediump float;
  attribute vec2 position;

  void main () {
    gl_Position = vec4(position, 0, 1);
  }
  `,

  frag: `
  precision mediump float;
  uniform float refPoints[3];
  varying vec3 fcolor;

  void main () {
    float a_value = refPoints[1];
    for(int i=0;i<3;++i) {
      a_value += 1.; // <-- this line is causing the error
    }
    gl_FragColor = vec4(1, 1, 1, 1);
  }
  `,
  attributes: {
    position: [
      [1, 0],
      [0, 1],
      [-1, -1]
    ],
  },
  uniforms : {
    "refPoints[0]" : 0.,
    "refPoints[1]" : 1.,
    "refPoints[2]" : 0.,
  },
  count: 3
})

regl.frame(function () {
  regl.clear({
    color: [0, 0, 0, 1],
    depth: 1
  })
  drawTriangle()
})
<script src="https://npmcdn.com/regl/dist/regl.js"></script>