在WebGL中为循环使用统一(或类似)的解决方法? [英] Workaround to use uniforms (or similar) in WebGL for loops?

查看:104
本文介绍了在WebGL中为循环使用统一(或类似)的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在WebGL中实现片段着色器,并且遇到了只能在for循环中使用常量表达式的局限性.有人对此有合适的解决方法吗?

I'm working on implementing a fragment shader in WebGL, and came across the limitation of being able to only use constant expressions in for loops. Does anyone have any suitable workarounds for this?

在我的特定情况下,我正在实现一个双边过滤器,当前在片段着色器中将窗口大小指定为const,但希望能够从JavaScript进行更改.统一不是常量,因此不能在for循环中使用,因此我正在寻找实现此目的的其他方法.

In my specific case, I'm implementing a bilateral filter, and currently have a window size specified as a const in my fragment shader, but would like to be able to change this from JavaScript. Uniforms aren't considered constants and thus can't be used in a for loop, so I'm looking for some other way of implementing this.

我唯一想到的是从JavaScript读取着色器源,对其进行解析,并将const的值替换为所需的窗口大小,然后重新编译着色器.这可以达到我的目的,但是我想知道是否有更简单的方法.

The only thing I can think of is to read the shader source from JavaScript, parse it and replace the value of the const with the desired window size, then recompile the shader. This would work for my purpose, but I'd like to know if there's an easier way.

推荐答案

如果您希望/需要具有动态更改循环长度的功能,则可以使用for循环计数到非常大的数字/无穷大和break一旦达到您的限制:

If you want/need the ability to dynamically change your loop length you may use a for loop counting to a very large number / infinity and break once your limit is reached:

uniform int loopLimit;

for (int i = 0; i < 10000; i++) {
  if (i == loopLimit) break;
  // Do your stuff
}  

这篇关于在WebGL中为循环使用统一(或类似)的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆