优化2D资产

本文介绍了如何优化2D资产,包括调整图像尺寸以减少内存带宽消耗,使用mipmap提高渲染速度和减少锯齿效应,以及处理透明通道以避免视觉伪影。这些方法能显著提升应用程序的启动、运行时性能和视觉体验。

Optimizing 2D Assets

优化2D资产

Optimizing the images used by Texture components in your scene can substantially improve both the startup and runtime performance of your application, as well as the visual quality in certain situations.

优化场景中纹理组件使用的图像可以显著提高应用程序的启动和运行时性能,以及某些情况下的视觉质量。

Motivation

动机

Unoptimized images can hurt the performance of your application in several ways:

未优化的图像可能会以多种方式影响应用程序的性能:

  • A large image takes more memory bandwidth when being traversed as a texture.
  • 当作为纹理遍历时,大图像占用更多内存带宽。
  • PNG and JPG compressed images must be decompressed before they can be sent to the graphics system.
  • PNG和JPG压缩图像必须先解压缩,然后才能发送到图形系统。
  • Poorly sized images degrade performance and quality on startup
  • 启动时图像尺寸太小会降低性能和质量
  • Sending uncompressed images to the graphics system takes longer, and they take up more graphics memory when there.
  • 将未压缩的图像发送到图形系统需要更长的时间,并且它们占用更多的图形内存。

Additionally, choosing an appropriate size for your images and using mipmaps can prevent aliasing issues if your image is going to be shown substantially smaller at some points.

​此外,为图像选择适当的大小并使用mipmap可以防止图像在某些点显示得小得多时出现混叠问题。

Reducing Image Dimensions

减少图像尺寸

Note: Make your image small, but with dimensions that are power of two.

注意:使图像较小,但尺寸为2的幂。

The first thing you need to do is resize your image to be almost as small as possible. Smaller images can be loaded faster by the graphics system as they need less bandwidth and memory. Think about the largest size that your image will ever be seen at, and resize your image down to that size. If your texture will be tiling at about 32 pixels per repetition, don't save it at 1024×1024.

您需要做的第一件事是调整图像的大小,使其尽可能小。图形系统可以更快地加载较小的图像,因为它们需要较少的带宽和内存。考虑一下您的图像将被看到的最大尺寸,然后将图像调整到该尺寸。如果纹理将以每次重复32像素左右的速度平铺,请不要将其保存为1024×1024。

However, note that both the horizontal and vertical dimensions of your image must be POT(power of two) to be stored on the GPU. Most of modern GPUs support NPOT(non-power of two) textures but many investigations show that using NPOT textures hurts about 30% of the performance. If you need to use NPOT textures, we recommend to make the dimensions multiple of 4. It might help your GPU to use its memory and cache efficiently and finally increase performance.

但是,请注意,图像的水平和垂直维度都必须是POT(2的幂),才能存储在GPU上。大多数现代GPU支持NPOT(非二次幂)纹理,但许多研究表明,使用NPOT纹理会损害大约30%的性能。如果您需要使用NPOT纹理,我们建议将维度设置为4的倍数。这可能有助于您的GPU有效地使用内存和缓存,并最终提高性能。

Note: If you are using mipmaps, there are further constraints on image dimensions.

​注意:如果您使用的是mipmap,则图像尺寸还有其他限制。

Using MipMaps

使用mipmap

Note: Use mipmaps when your image may be seen smaller than the original size, including portions in perspective.

注意:当您的图像可能小于原始大小(包括透视图中的部分)时,请使用mipmap。

Enabling mipmaps creates many smaller copies of the image at an additional 1/3 memory usage. Each mipmap dimension is halved from the one preceding it, and is downsized in image editing tool with good image resampling. The result speeds up rendering, decreases the time that the GPU uses for texture lookup, and also reduces aliasing artifacts such as moiré effects or texture subsampling.

​启用mipmap将以额外1/3的内存使用率创建图像的许多较小副本。每个mipmap维度从之前的维度减半,并在图像编辑工具中缩小,具有良好的图像重采样。结果加快了渲染速度,减少了GPU用于纹理查找的时间,还减少了锯齿瑕疵,如云纹效果或纹理子采样。

To provide mipmaps at build time, the texture needs to be in a container format like KTX, as standard image formats do not have the capability of storing mipmaps.

要在构建时提供mipmap,纹理需要采用容器格式,如KTX,因为标准图像格式不具备存储mipmap的能力。

It is possible to generate mipmaps at runtime by setting the property Texture::generateMipmaps to true.

​通过将属性Texture::generateMipmaps设置为true,可以在运行时生成MipMap。

Note: Mipmaps require specific image dimensions to work correctly at all levels, depending on which encoding is used.

注意:MIPMap需要特定的图像维度才能在所有级别正常工作,具体取决于使用的编码。

MipMaps and Image Dimensions

MIPMap和图像维度

If the original image dimensions are represented by widthheight, and depth, the number of mipmap levels will be 1 + floor(log2(max(width, height, depth))) and each levels' dimensions will be max(1, width_prev >> 1)max(1, height_prev >> 1), and max(1, depth_prev >> 1).

如果原始图像尺寸由widthheightdepth表示,则mipmap级别的数量将为1 + floor(log2(max(width, height, depth))),每个级别的尺寸将为max(1, width_prev >> 1)max(1, height_prev >> 1)max(1, depth_prev >> 1)

Saving Alpha Channels

保存Alpha通道

Most of the time when dealing with images with semi-transparent regions, it is easier to use image editing tool's transparent layers when editing such images. If you choose a compressed encoding that supports alpha, the transparent regions of your scene are properly used for the alpha information.

大多数情况下,当处理具有半透明区域的图像时,在编辑此类图像时,使用图像编辑工具的透明层更容易。如果选择支持alpha的压缩编码,场景的透明区域将正确用于alpha信息。

However, in certain cases you need to control the RGB values of fully transparent pixels. Specifically, you can see visual artifacts if:

但是,在某些情况下,您需要控制完全透明像素的RGB值。具体而言,您可以在以下情况下看到视觉伪影:

  • Any portion of your image will ever be seen at a size larger than saved.
  • 图像的任何部分都将以大于保存的大小显示。
  • You have fully transparent pixels in your image next to rather opaque pixels.
  • 图像中的完全透明像素与不透明像素相邻。

In this case, the texture interpolation between a fully-transparent pixel and its neighboring somewhat-opaque pixel blends the RGB values between the two. If you use the image editing tool's transparent layers, the RGB values for certain transparent pixels are saved as white, and you will thus see white fringing at the edges of your transparent regions.

在这种情况下,完全透明像素与其相邻的略微不透明像素之间的纹理插值会混合两者之间的RGB值。如果使用图像编辑工具的透明层,某些透明像素的RGB值将保存为白色,因此您将在透明区域的边缘看到白色条纹。

For such cases, instead of creating a semi-transparent layer in your image editing tool, create a layer with no transparency at all, setting the RGB value for every pixel you care about. Then, save the alpha information in a fourth channel.

在这种情况下,不要在图像编辑工具中创建半透明层,而是创建一个完全没有透明度的层,为您关心的每个像素设置RGB值。然后,将alpha信息保存在第四通道中。

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值