如何使用 texSubImage2D 在 webgl 中显示精灵?
我可以通过调用 gl.texImage2D 成功显示整个精灵 (32x512):
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
正如我预期的那样,它被水平挤压,但至少在屏幕上呈现。我试图仅显示工作表中的第一个 32x32 精灵,并且我假设我可以简单地使用 gl.texSubImage2D 来实现此效果。我尝试用 texSubImage2D (修改参数)简单替换 texImage2D,但屏幕上只是出现一个黑框。这是我正在使用的代码:
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 32, 32, gl.RGBA, gl.UNSIGNED_BYTE, image);
我是否遗漏了一些关于texSubImage2D 的实现?我还需要执行其他步骤吗?或者 texSubImage2D 不是制作精灵表的正确方法?
I can display my entire sprite (32x512) successfully with this call to gl.texImage2D:
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
It's squished horizontally, like I expected, but it renders on the screen at least. I'm trying to only display the first 32x32 sprite in the sheet and I assumed that I could simply use gl.texSubImage2D to achieve this effect. I tried a simple replacement of texImage2D with texSubImage2D (with modified parameters) but I just get a black box on the screen. Here's the code I'm using:
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 32, 32, gl.RGBA, gl.UNSIGNED_BYTE, image);
Am I missing something about the implementation of texSubImage2D? Is there some other step I have to do? Or is texSubImage2D not the right way to do sprite sheets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
texSubImage2D
不是您想要的功能。您遇到了三个问题:texSubImage2D
不会将image
的子集复制到 GL 纹理中。它将整个image
复制到GL纹理顶部给定的偏移处。texSubImage2D
只能修改现有的纹理数据,并且除非先为 GL 纹理调用texImage2D
,否则会造成混乱。texSubImage2D
的调用样式需要像素数组而不是HTMLImageElement
。这些方法有四种可能的签名:
要使用图像的前 32x32 像素创建纹理,请执行以下操作:
texSubImage2D
is not the function you want. You're running into three problems:texSubImage2D
does not copy a subset ofimage
into the GL texture. It copies the entirety ofimage
on top of the GL texture at a given offset.texSubImage2D
can only modify existing texture data, and will mess up unlesstexImage2D
has been called first for the GL texture.texSubImage2D
expects a pixel array instead of anHTMLImageElement
.There are four possible signatures for these methods:
To create a texture using the first 32x32 pixels of your image, do something like this instead: