GPUTexture
注意:此功能在 Web Workers 中可用。
GPUTexture 接口是 WebGPU API 的一部分,它表示一个容器,用于存储用于 GPU 渲染操作的 1D、2D 或 3D 数据数组,例如图像。
GPUTexture 对象实例是通过 GPUDevice.createTexture() 方法创建的。
实例属性
depthOrArrayLayers只读-
一个表示
GPUTexture的深度或层数的数字(像素或层数)。 dimension只读-
一个枚举值,表示每个
GPUTexture子资源纹素集的维度。 format只读-
一个表示
GPUTexture格式的枚举值。有关所有可能值的详细信息,请参阅规范的 纹理格式 部分。 height只读-
一个表示
GPUTexture高度的数字(像素)。 label-
一个字符串,提供可用于识别对象的标签,例如在
GPUError消息或控制台警告中。 mipLevelCount只读-
一个表示
GPUTexture的 mip 级别数量的数字。 sampleCount只读-
一个表示
GPUTexture的采样数的数字。 usage只读-
表示
GPUTexture允许用法的 按位标志。 width只读-
一个表示
GPUTexture宽度的数字(像素)。
实例方法
createView()-
创建一个
GPUTextureView,表示GPUTexture的特定视图。 destroy()-
销毁
GPUTexture。
示例
在 WebGPU 示例 纹理立方体示例 中,通过以下方式创建用于立方体面的纹理:
- 将图像加载到
HTMLImageElement中,并使用createImageBitmap()创建图像位图。 - 使用
createTexture()创建新的GPUTexture。 - 使用
GPUQueue.copyExternalImageToTexture()将图像位图复制到纹理中。
js
// …
let cubeTexture;
{
const img = document.createElement("img");
img.src = new URL(
"../../../assets/img/Di-3d.png",
import.meta.url,
).toString();
await img.decode();
const imageBitmap = await createImageBitmap(img);
cubeTexture = device.createTexture({
size: [imageBitmap.width, imageBitmap.height, 1],
format: "rgba8unorm",
usage:
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.COPY_DST |
GPUTextureUsage.RENDER_ATTACHMENT,
});
device.queue.copyExternalImageToTexture(
{ source: imageBitmap },
{ texture: cubeTexture },
[imageBitmap.width, imageBitmap.height],
);
}
// …
规范
| 规范 |
|---|
| WebGPU # gputexture |
浏览器兼容性
加载中…