在 WebGL 中用纹理替换颜色
在视频游戏中,仅应用颜色来帮助加快加载过程。纹理准备好后,它们会替换当前的颜色。有没有办法在 WebGL
中做到这一点?到目前为止我看到的所有教程都只展示了如何加载颜色或纹理(不是一个接一个)。
我猜想每个形状的缓冲区在其纹理完全加载后都需要更改。我假设这是通过 AJAX 调用来确定纹理可用的,然后通过某种 JavaScript 函数应用。 WebGL
是否有内置的方法来执行此操作,而无需复杂的图像加载过程?
In video games only color is applied to help speed up the loading process. After textures are ready, they replace the current colors. Is there a way to do this in WebGL
? All the tutorials I've seen so far only show how to load in color or texture (not one after the other).
I'd guess that the buffer for each shape needs to be altered after its texture is fully loaded. I would assume this is keyed via an AJAX call that the texture is available, then applied via some kind of JavaScript function. Does WebGL
have a built in way of doing this without a complicated image loading process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我见过的大多数具有您所描述的行为的游戏中,它们通常会从每个顶点着色或非常低分辨率的纹理开始,并在可用时“混合”到完整的纹理。这种平滑的过渡很棘手,但如果您想要的只是从一个到另一个的快速“弹出”,那么应该不会太麻烦。
我采取的基本路线是创建一个具有纹理坐标和颜色信息以及两个不同着色器的顶点缓冲区。一个着色器将使用颜色信息,另一个着色器将忽略它并使用纹理。一旦纹理准备好,您将向网格发出信号以开始使用基于纹理的网格。
至于检测图像负载,这根本不难,您甚至不需要 AJAX:
我不确定在不发布非常大的代码块的情况下我可以在这个主题上提供多少帮助,但如果您仍在努力,我可以详细介绍其他一些部分。
In most games that I've seen with the behavior you describe they'll typically start with either per-vertex coloring or a VERY low res texture and "blend up" to the full texture when it becomes available. That sort of smooth transition is tricky, but if all you want is a quick "pop" from one to the other it shouldn't be too much trouble.
The basic route I would take is to create a vertex buffer that has both texture coord and color information, as well as two different shaders. One shader will use the color information, the other will ignore it and use the texture instead. You would signal the mesh to start using the texture-based one as soon as the texture is ready.
As for detecting the image load, that's not hard at all and you don't even need AJAX for it:
I'm not sure how much more help I can give on this subject without posting really large code blocks, but if you're still struggling I can detail some of the other parts.
我将采取的方法如下
这是 http://webglsamples.googlecode.com 尽管它们都默认为蓝色纹理,但仍然可以工作。
您可以轻松扩展该想法以使用纯色,加载低分辨率纹理,然后在完成后加载高分辨率纹理。
注意:上面的代码假设您正在加载 2 的幂纹理。如果没有,您需要正确设置纹理参数。
The approach I would take is as follows
This is how most of the samples on http://webglsamples.googlecode.com work although they all default to blue textures.
You could easily extend that idea to use a solid color, the load a low-res texture, then when that finishes load a high-res texture.
Note: the code above assumes you are loading power-of-2 textures. If not you'll need to setup your texture parameters correctly.
这实际上非常简单,WebGL 没有任何专门针对此的功能,这是您可以通过它作为 DOM API 免费获得的东西。当您加载图像时,无论如何您都必须实现它们的“onload”回调,因为图像加载是异步的。因此,只需将需要运行的任何代码放入“onload”回调中即可从纯色切换到纹理。
It's actually very easy, without WebGL having any feature specifically for this, this is something that you get for free just from it being a DOM API. When you load images, anyway you have to implement their 'onload' callback as image loading is asynchronous. So just put in that 'onload' callback whatever code needs to be run to switch from the solid color to the texture.