Unity - 使用 Compute Shader 生成自定义 Mipmap

Unity - Generate Custom Mipmap with Compute Shader

Dec 11, 24

Mipmap 在实时渲染中有非常多很妙的作用, 例如高性能模糊, 高性能渐变模糊等等等。 Unity 的 Mipmap实现比较简单, 有时候我们需要使用一些特殊的 Downsample 算法来生成 Mipmap。 可以使用 Compute Shader 来实现这个功能。

Mipmap has many wonderful uses in real-time rendering, such as high-performance blur, high-performance gradient blur, etc. Unity's Mipmap implementation is relatively simple, sometimes we need to use some special Downsample algorithm to generate Mipmap. In this case, we can use Compute Shader to implement this functionality.

步骤

Steps

1. 创建 / 导入 图片材质
1. Create / Import Image Texture

首先, 我们需要一个图片材质, 用于生成 Mipmap。 你可以使用 Unity 的内置图片导入功能导入图片。接 下来需要为 Compute Shader 创建一个 Render Texture, 并在属性面板中设置作以下修改:

  • Size - 修改为原始图片大小
  • Color Format - 修改为与原始图片格式相同
  • Depth Stencil - 关闭
  • Mipmap - 打开
  • Mipmap/Auto Generate - 关闭
  • Random Write - 打开
  • Filter Mode - 修改为 Point

其他保持默认即可。

First, we need an image texture to generate Mipmap. You can use Unity's built-in image import feature to import the image. Next, create a Render Texture for the Compute Shader, and make the following modifications in the property panel:

  • Size - Change to the size of the original image
  • Color Format - Change to the same format as the original image
  • Depth Stencil - Off
  • Mipmap - On
  • Mipmap/Auto Generate - Off
  • Random Write - On
  • Filter Mode - Change to Point