Ошибка Glsl при попытке увеличить переменную внутри цикла for - PullRequest
0 голосов
/ 07 марта 2019

Контекст: я передаю равномерный массив float моему фрагментному шейдеру, затем инициализирую float значением из этого массива и, наконец, пытаюсь увеличить этот float в цикле for с помощью следующего кода:

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 голосов
/ 07 марта 2019

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>
...