javascript 如何在 webgl 着色器中使用 console.log?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17396929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to console.log in webgl shaders?
提问by Kirill Ivlev
I'm trying to understand how to simulate console.log in webgl shaders which are written in GLSL. It's easy to get error messages but I can't get how to print custom messages.
我试图了解如何在用 GLSL 编写的 webgl 着色器中模拟 console.log。很容易收到错误消息,但我不知道如何打印自定义消息。
Basically I want to print stuff in the browser's console:
基本上我想在浏览器的控制台中打印内容:
<script id="shader-fs1" type="x-shader/x-fragment">
void main(void)
{
//console.log doesn't work here since it's GLSL not javascript
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>
Any suggestions?
有什么建议?
回答by Abstract Algorithm
After compiling shader you can do something like:
编译着色器后,您可以执行以下操作:
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
}
And it will show you any error messages during compiling. GLSL cannot send data back to program in any other form but framebuffer/texture, so you can only check what's happening by inspecting output colors. WebGL inspector might me useful, as pointed by Michael, but not that much for shaders, but for general debugging of webGL apps
它会在编译过程中向您显示任何错误消息。GLSL 不能以帧缓冲区/纹理以外的任何其他形式将数据发送回程序,因此您只能通过检查输出颜色来检查发生了什么。正如 Michael 所指出的,WebGL 检查器可能对我有用,但对于着色器来说不是那么有用,但对于 webGL 应用程序的一般调试
回答by Michael McGuire
Not sure if that is possible, but you may want to check out the WebGL Inspector libraryfor debugging purposes.
不确定这是否可行,但您可能需要查看WebGL Inspector 库以进行调试。
回答by havarc
Currently there is no known way to output data from GLSL in WebGL except via it's purposed result (screen/image color). Unless you already do I would suggest you check out Learning WebGL, also kick.jsmight be useful to you.
目前没有已知的方法可以在 WebGL 中从 GLSL 输出数据,除非通过它的预期结果(屏幕/图像颜色)。除非您已经这样做了,否则我建议您查看Learning WebGL,kick.js也可能对您有用。