【问题标题】:Passing Array Of Vectors To Shader将向量数组传递给着色器
【发布时间】:2021-01-07 05:01:36
【问题描述】:

试图将一组 vec3s 传递给着色器,Firefox 给了我这个警告:

WebGL 警告:统一设置器:(统一 u_colors[0])“值”长度 (4) 必须是 大小的正整数倍。

此外,它只呈现黑色,而不是给定的颜色。

const colrs = [
    [239, 71, 111],
    [255, 209, 102],
    [6, 214, 160],
    [17, 138, 178]
  ],
  dmnsn = [1200, 300],
  glCtx = (() => twgl.getContext(document.createElement("canvas")))(
    twgl.setDefaults({ attribPrefix: "a_" })
  );

var bfInf = twgl.primitives.createXYQuadBufferInfo(glCtx),
  pgInf = twgl.createProgramInfo(glCtx, ["vs", "fs"]);
function rendr(fbi, pi, u) {
  twgl.bindFramebufferInfo(glCtx, fbi);
  twgl.drawObjectList(glCtx, [
    {
      programInfo: pi,
      bufferInfo: bfInf,
      uniforms: u
    }
  ]);
}
function prepr() {
  glCtx.canvas.width = dmnsn[0];
  glCtx.canvas.height = dmnsn[1];
  document.body.append(glCtx.canvas);
  rendr(null, pgInf, { u_colors: colrs });
}
prepr();
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<script id="vs" type="x-shader/x-vertex">
  attribute vec4 a_position;
  attribute vec2 a_texcoord;

  varying vec2 v_texcoord;

  void main() {
    v_texcoord = a_texcoord;
    gl_Position = a_position;
  }
</script>
<script id="fs" type="x-shader/x-fragment">
  precision highp float;
  
  uniform vec3 u_colors[4];

  void main() {
    gl_FragColor= vec4(u_colors[0], 1.0);
  }
</script> 

【问题讨论】:

    标签: firefox webgl twgl.js


    【解决方案1】:

    WebGL 不需要数组。

    const someArrayWith4ArraysOf3Values = [
      [11, 22, 33],
      [44, 55, 66],
      [77, 88, 99],
      [10, 11, 12],
    ];
    gl.uniform3fv(some4ElementVec3Uniform, someArrayWith4ArraysOf3Values); // bad
    

    它需要一个平面数组。

    const someArrayWith12Values = [
      11, 22, 33,
      44, 55, 66,
      77, 88, 99,
      10, 11, 12,
    ];
    gl.uniform3fv(some4ElementVec3Uniform, someArrayWith12Values); // good
    

    此外,着色器中的颜色是从 0 到 1 的浮点值,因此您可能希望在着色器中将颜色设置为 0 到 1 之间的值或除以 255

    const colrs = [
        [1, 0.7, 0.5],
        [255, 209, 102],
        [6, 214, 160],
        [17, 138, 178]
      ].flat(),
      dmnsn = [1200, 300],
      glCtx = (() => twgl.getContext(document.createElement("canvas")))(
        twgl.setDefaults({ attribPrefix: "a_" })
      );
    
    var bfInf = twgl.primitives.createXYQuadBufferInfo(glCtx),
      pgInf = twgl.createProgramInfo(glCtx, ["vs", "fs"]);
    function rendr(fbi, pi, u) {
      twgl.bindFramebufferInfo(glCtx, fbi);
      twgl.drawObjectList(glCtx, [
        {
          programInfo: pi,
          bufferInfo: bfInf,
          uniforms: u
        }
      ]);
    }
    function prepr() {
      glCtx.canvas.width = dmnsn[0];
      glCtx.canvas.height = dmnsn[1];
      document.body.append(glCtx.canvas);
      rendr(null, pgInf, { u_colors: colrs });
    }
    prepr();
    <script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
    <script id="vs" type="x-shader/x-vertex">
      attribute vec4 a_position;
      attribute vec2 a_texcoord;
    
      varying vec2 v_texcoord;
    
      void main() {
        v_texcoord = a_texcoord;
        gl_Position = a_position;
      }
    </script>
    <script id="fs" type="x-shader/x-fragment">
      precision highp float;
      
      uniform vec3 u_colors[4];
    
      void main() {
        gl_FragColor=vec4(u_colors[0], 1.0);
      }
    </script>

    【讨论】:

    • 谢谢!哦,是的,我忘了颜色是浮动的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2021-11-19
    • 2014-10-04
    相关资源
    最近更新 更多