我决定学习 Dart,我的项目是一个小型的 webgl 游戏。
不过,使用具有异步操作和功能的单线程对我来说是新的,虽然我理解这些概念,但我发现很难知道如何使用它们。
在我的游戏中我想加载一个 webgl GLSL 程序。要创建这个程序,我首先必须从文件加载顶点着色器和片段着色器。所以我写了这个似乎可以正常加载和编译着色器。
我遇到的问题是如何知道两个着色器何时加载,以便我可以从两个加载的着色器创建“程序”对象。就像我可以在将来从 HttpRequest.getString 返回时使用 .then 一样,我需要以某种方式做同样的事情,并有一个 .then 在加载片段和顶点着色器时运行。
我确信我在这里错过了一些主要且简单的东西,但这对我来说是新的,我正在努力了解如何使用它。 (这很好,学习新东西很好!)
//
// Start the process of loading the shaders.
//
void initShaders()
{
Shader vs = gl.createShader(VERTEX_SHADER);
Shader fs = gl.createShader(FRAGMENT_SHADER);
var vsFuture = HttpRequest.getString('basic.vert');
var fsFuture = HttpRequest.getString('basic.frag');
//
// When we have the vertex shader source, compile it
//
vsFuture.then((src) {
gl.shaderSource(vs, src);
gl.compileShader(vs);
if (!gl.getShaderParameter(vs, COMPILE_STATUS)) {
throw new Exception(gl.getShaderInfoLog(vs));
}
print ('Vertex shader compiled');
});
//
// When we have the fragment shader source, compile it
//
fsFuture.then((src) {
gl.shaderSource(fs, src);
gl.compileShader(fs);
if (!gl.getShaderParameter(fs, COMPILE_STATUS)) {
throw new Exception(gl.getShaderInfoLog(fs));
}
print ('Fragment shader compiled');
});
//
// When both the fragment shader and the vertex shader are
// compiled, we need to link them. But how do we know??
//
***something... to make this be called when both the vertex and
fragment shaders are compiled...***
{
program = gl.createProgram();
gl.attachShader(program, fs);
gl.attachShader(program, ps);
gl.linkProgram(program);
}
全部致电then
返回一个Future
then回调执行后触发。所以编译步骤有两个未来。之后您可以使用Future.wait https://api.dartlang.org/docs/channels/stable/latest/dart_async/Future.html#wait等待期货清单。这wait
函数返回一个Future
在所有输入 future 完成后触发。
像这样的东西(不完整,但了解它是如何工作的):
var vsCompiledFuture = vsFuture.then((src) {
// Compile
});
var fsCompiledFuture = fsFuture.then((src) {
// Compile
});
Future.wait([vsCompiledFuture, fsCompiledFuture]).then((_) {
// Link here...
});
如果您从未来的回调中返回已编译的着色器,Future.wait
回调接收一个包含所有 future 的所有结果的列表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)