GPUTexture

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 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 示例 紋理立方體示例 中,透過以下方式建立用於立方體面的紋理:

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

瀏覽器相容性

另見