导入图像

支持的图像格式

Godot 可以导入以下图像格式:

  • BMP(.bmp)——不支持每像素 16 位的图像。只支持每像素 1 位、4 位、8 位、24 位和 32 位的图像。

  • DirectDraw Surface(.dds)——如果纹理中存在 mipmap,则直接加载。可以使用自定义 mipmap 制作特效。

  • Khronos Texture (.ktx) - Decoding is done using libktx. Only supports 2D images. Cubemaps, texture arrays and de-padding are not supported.

  • OpenEXR(.exr)——支持 HDR(强烈推荐使用在全景天空上)。

  • Radiance HDR(.hdr)——支持 HDR(强烈推荐使用在全景天空上)。

  • JPEG(.jpg.jpeg)——由于该格式的限制,不支持透明度。

  • PNG(.png)——导入时精度限制为每个通道 8 位(无 HDR 图像)。

  • Truevision Targa(.tga

  • SVG(.svg)——SVG 在导入时使用 ThorVG 进行栅格化。支持并不完善 ;复杂的矢量图可能无法正确渲染。你可以使用其`基于网络的查看器 <https://www.thorvg.org/viewer>`__ 来检查 ThorVG 是否可以正确渲染某个向量。对于复杂的矢量,使用 Inkscape 将它们渲染为 PNG 会是更好的解决方案。由于 Inkscape 具有 命令行界面 ,这套流程还可以自动化处理。

  • WebP(.webp)——WebP 文件支持透明,也支持有损和无损压缩。精度限制是每通道 8 位。

备注

如果你从源代码编译 Godot 编辑器时禁用了特定的模块,某些格式可能无法使用。

导入纹理

Godot 中的默认操作是将图像导入为纹理。纹理存储在显存中。纹理的像素数据无法直接从 CPU 访问,除非在脚本中将它们转换回 Image。这就是使绘制纹理变得高效的原因。

在文件系统面板中选择图像后,可以调整十多个导入选项:

在文件系统面板中选择图像后,导入面板中的导入选项

在文件系统面板中选择图像后,导入扩展面板中的导入选项。其中一些选项仅在某些压缩模式下可见。

更改导入类型

可以在“导入”面板中选择其他导入资源的类型:

  • BitMap: 1-bit monochrome texture (intended to be used as a click mask in TextureButton and TouchScreenButton). This resource type cannot be displayed directly onto 2D or 3D nodes, but the pixel values can be queried from a script using get_bit.

  • Cubemap: Import the texture as a 6-sided cubemap, with interpolation between the cubemap’s sides (seamless cubemaps), which can be sampled in custom shaders.

  • CubemapArray: Import the texture as a collection of 6-sided cubemaps, which can be sampled in custom shaders. This resource type can only be displayed when using the Forward+ or Forward Mobile rendering methods, not Compatibility.

  • Font Data (Monospace Image Font): Import the image as a bitmap font where all characters have the same width. See 使用字体.

  • Image: Import the image as-is. This resource type cannot be displayed directly onto 2D or 3D nodes, but the pixel values can be queried from a script using get_pixel.

  • Texture2D: Import the image as a 2-dimensional texture, suited for display on 2D and 3D surfaces. This is the default import mode.

  • Texture2DArray: Import the image as a collection of 2-dimensional textures. Texture2DArray is similar to a 3-dimensional texture, but without interpolation between layers. Built-in 2D and 3D shaders cannot display texture arrays, so you must create a custom shader in 2D or 3D to display a texture from a texture array.

  • Texture3D: Import the image as a 3-dimensional texture. This is not a 2D texture applied onto a 3D surface. Texture3D is similar to a texture array, but with interpolation between layers. Texture3D is typically used for FogMaterial density maps in volumetric fog, Environment 3D LUT color correction and custom shaders.

  • TextureAtlas: Import the image as an atlas of different textures. Can be used to reduce memory usage for animated 2D sprites. Only supported in 2D due to missing support in built-in 3D shaders.

检测 3D

默认的导入选项(不带 mipmap 并且使用 Lossless 压缩)适合 2D,对于大多数 3D 项目而言并不理想。检测 3D能够让 Godot 关注纹理在 3D 场景中的使用(例如用作 BaseMaterial3D 的纹理)。一旦使用,就会将部分导入选项进行修改,这样纹理标志就对 3D 更友好。除非修改了 Detect 3D > Compress To,否则此时就会启用 mipmap 并将压缩模式修改为 VRAM Compressed。纹理会自动进行重新导入。

检测到纹理在 3D 中使用时会在“输出”面板中打印一个消息。

如果纹理检测到在 3D 中使用后遇到了质量问题(例如像素风纹理),请在用于 3D 之前修改 Detect 3D > Compress To 选项,或者在用于 3D 之后将 压缩 > 模式 修改为 Lossless。这样比禁用检测 3D 更好,因为仍然会启用 mipmap 生成,从远处观察纹理就不会感觉模糊。

导入选项

参见

在 Godot 4.0 中,对纹理过滤模式以及重复模式的修改不再位于导入选项中。

现在,2D 中需要在 CanvasItem 的属性中对纹理过滤模式和重复模式进行修改(项目设置中可以设置默认值),3D 中则是针对材质进行配置。在自定义着色器中,sampler2D uniform 的过滤和重复模式使用提示修改,见 着色语言 文档。

压缩 > 模式

图像是游戏中最大的资产之一。要实现高效的处理,就需要进行压缩。Godot 提供了很多压缩方法,可以根据实际情况选用。

  • Lossless:无损压缩。这是 2D 资产的默认压缩模式,也是最常用的压缩模式。显示资产时不会有任何失真,磁盘压缩也比较合适。但如果与 VRAM 压缩相比,使用的显存就明显要更多。这也是像素画的推荐设置。

  • Lossy:有损压缩。适合较大的 2D 资产。会有一些失真,但是比 VRAM 压缩要少,文件大小比无损压缩以及 VRAM 未压缩要小好几倍。这个模式不会降低显存占用;与无损压缩和 VRAM 未压缩相同。

  • VRAM Compressed:VRAM 压缩。这是 3D 资产的默认压缩模式,也是最常用的压缩模式。会降低磁盘上的大小,显存占用也会显著降低(通常是 4 到 6 倍)。应该避免在 2D 中使用这个模式,因为会有明显的失真,在低分辨率纹理上尤为明显。

  • VRAM Uncompressed:VRAM 未压缩。仅适用于不能压缩的格式,例如原始浮点数图像。

  • Basis Universal:这也是一种 VRAM 压缩模式,编码后的纹理格式为能够在加载时转码为大多数 GPU 压缩格式。这样得到的文件就很小,能够利用 VRAM 压缩,但相对于 VRAM 压缩而言质量较差,压缩耗时也更长。显存占用通常和 VRAM 压缩相同。Basis Universal 不支持浮点数图像格式(引擎会在内部回退至 VRAM 压缩)。

备注

即便在 3D 中,“像素画”纹理也应该禁用 VRAM 压缩,因为压缩会对外观产生负面影响,较低的分辨率也无法得到显著的性能提升。

这张表对这五个选项进行了横向比较,列出了各自的优缺点(good = 最好、bad = 最差):

压缩模式

Lossless

Lossy

VRAM Compressed

VRAM Uncompressed

Basis Universal

描述

存储为无损 WebP / PNG

存储为有损 WebP

根据平台不同,存储为 S3TC、BPTC 或 ETC2

存储为原始像素

转码至 VRAM Compressed 格式

磁盘占用

regular

good 非常小

regular

bad

good 非常小

内存占用

bad

bad

good

bad

good

性能

regular 普通

regular 普通

good

regular 普通

good

质量损失

good

regular 轻微

bad 中等

good

bad 中等

加载时间

bad

bad

good

regular 普通

regular 普通

单张 RGBA8 纹理启用 mipmap 后的预估内存占用:

纹理大小

Lossless

Lossy

VRAM Compressed

VRAM Uncompressed

Basis Universal

128×128

good 85 KiB

good 85 KiB

good 21 KiB

good 85 KiB

good 21 KiB

256×256

good 341 KiB

good 341 KiB

good 85 KiB

good 341 KiB

good 85 KiB

512×512

good 1.33 MiB

good 1.33 MiB

good 341 KiB

good 1.33 MiB

good 341 KiB

1024×1024

regular 5.33 MiB

regular 5.33 MiB

good 1.33 MiB

regular 5.33 MiB

good 1.33 MiB

2048×2048

bad 21.33 MiB

bad 21.33 MiB

regular 5.33 MiB

bad 21.33 MiB

regular 5.33 MiB

4096×4096

bad 85.33 MiB

bad 85.33 MiB

bad 21.33 MiB

bad 85.33 MiB

bad 21.33 MiB

备注

In the above table, memory usage will be reduced by 25% for images that do not have an alpha channel (RGB8). Memory usage will be further decreased by 25% for images that have mipmaps disabled.

Notice how at larger resolutions, the impact of VRAM compression is much greater. With a 4:1 compression ratio (6:1 for opaque textures with S3TC), VRAM compression effectively allows a texture to be twice as large on each axis, while using the same amount of memory on the GPU.

VRAM compression also reduces the memory bandwidth required to sample the texture, which can speed up rendering in memory bandwidth-constrained scenarios (which are frequent on integrated graphics and mobile). These factors combined make VRAM compression a must-have for 3D games with high-resolution textures.

You can preview how much memory a texture takes by double-clicking it in the FileSystem dock, then looking at the Inspector:

Previewing a texture in the Inspector

Previewing a texture in the Inspector. Credit: Red Brick 03 - Poly Haven

Compress > High Quality

备注

High-quality VRAM texture compression is only supported in the Forward+ and Forward Mobile rendering methods.

When using the Compatibility rendering method, this option is always considered disabled.

If enabled, uses BPTC compression on desktop platforms and ASTC compression on mobile platforms. When using BPTC, BC7 is used for SDR textures and BC6H is used for HDR textures.

If disabled (default), uses the faster but lower-quality S3TC compression on desktop platforms and ETC2 on mobile/web platforms. When using S3TC, DXT1 (BC1) is used for opaque textures and DXT5 (BC3) is used for transparent or normal map (RGTC) textures.

BPTC and ASTC support VRAM compression for HDR textures, but S3TC and ETC2 do not (see HDR Compression below).

Compress > HDR Compression

备注

This option only has an effect on textures that are imported as HDR formats in Godot (.hdr and .exr files).

If set to Disabled, never uses VRAM compression for HDR textures, regardless of whether they’re opaque or transparent. Instead, the texture is converted to RGBE9995 (9-bits per channel + 5-bit exponent = 32 bits per pixel) to reduce memory usage compared to a half-float or single-precision float image format.

If set to Opaque Only (default), only uses VRAM compression for opaque HDR textures. This is due to a limitation of HDR formats, as there is no VRAM-compressed HDR format that supports transparency at the same time.

If set to Always, will force VRAM compression even for HDR textures with an alpha channel. To perform this, the alpha channel is discarded on import.

Compress > Normal Map

When using a texture as normal map, only the red and green channels are required. Given regular texture compression algorithms produce artifacts that don’t look that nice in normal maps, the RGTC compression format is the best fit for this data. Forcing this option to Enable will make Godot import the image as RGTC compressed. By default, it’s set to Detect. This means that if the texture is ever detected to be used as a normal map, it will be changed to Enable and reimported automatically.

Note that RGTC compression affects the resulting normal map image. You will have to adjust custom shaders that use the normal map’s blue channel to take this into account. Built-in material shaders already ignore the blue channel in a normal map (regardless of the actual normal map’s contents).

In the example below, the normal map with RGTC compression is able to preserve its detail much better, while using the same amount of memory as a standard RGBA VRAM-compressed texture:

Normal map with standard VRAM compression (left) and with RGTC VRAM compression (right)

Normal map with standard VRAM compression (left) and with RGTC VRAM compression (right)

备注

Godot requires the normal map to use the X+, Y+ and Z+ coordinates, which is known as an OpenGL-style normal map. If you’ve imported a material made to be used with another engine, it may be DirectX-style. In this case, the normal map needs to be converted by enabling the Normal Map Invert Y import option.

可以在 这里 找到关于法线贴图(包括常见引擎的坐标顺序表)的更多信息.

Compress > Channel Pack

If set to sRGB Friendly (default), prevents the RG color format from being used as it does not support sRGB color.

If set to Optimized, allows the RG color format to be used if the texture does not use the blue channel.

A third option Normal Map (RG Channels) is only available in layered textures (Cubemap, CubemapArray, Texture2DArray and Texture3D). This forces all layers from the texture to be imported with the RG color format to reduce memory usage, with only the red and green channels preserved. This only has an effect on textures with the VRAM Compressed or Basis Universal compression modes.

Mipmaps > Generate

If enabled, smaller versions of the texture are generated on import. For example, a 64×64 texture will generate 6 mipmaps (32×32, 16×16, 8×8, 4×4, 2×2, 1×1). This has several benefits:

  • Textures will not become grainy in the distance (in 3D), or if scaled down due to camera zoom or CanvasItem scale (in 2D).

  • Performance will improve if the texture is displayed in the distance, since sampling smaller versions of the original texture is faster and requires less memory bandwidth.

mipmaps 的缺点是会增加大约 33% 的内存使用量。

It’s recommended to enable mipmaps in 3D. However, in 2D, this should only be enabled if your project visibly benefits from having mipmaps enabled. If the camera never zooms out significantly, there won’t be a benefit to enabling mipmaps but memory usage will increase.

Mipmaps > Limit

警告

Mipmaps > Limit is currently not implemented and has no effect when changed.

If set to a value greater than -1, limits the maximum number of mipmaps that can be generated. This can be decreased if you don’t want textures to become too low-resolution at extreme distances, at the cost of some graininess.

Roughness > Mode

The color channel to consider as a roughness map in this texture. Only effective if Roughness > Src Normal is not empty.

Rougness > Src Normal

The path to the texture to consider as a normal map for roughness filtering on import. Specifying this can help decrease specular aliasing slightly in 3D.

Roughness filtering on import is only used in 3D rendering, not 2D.

Process > Fix Alpha Border

This puts pixels of the same surrounding color in transition from transparent to opaque areas. For textures displayed with bilinear filtering, this helps mitigate the outline effect when exporting images from an image editor.

../../_images/fixedborder.png

It’s recommended to leave this enabled (as it is by default), unless this causes issues for a particular image.

Process > Premult Alpha

另一种修复深色边界的方法是使用预乘 alpha。启用该选项后,纹理将被转换为此格式。请注意,在画布项(canvas items)上使用预乘 ALPHA 混合模式需要先创建材质,并开启“PREMULT ALPHA”混合模式:

  • In 2D, a CanvasItemMaterial will need to be created and configured to use the Premul Alpha blend mode on CanvasItems that use this texture.

  • In 3D, there is no support for premultiplied alpha blend mode yet, so this option is only suited for 2D.

Process > Normal Map Invert Y

Godot requires the normal map to use the X+, Y+ and Z+ coordinates, which is known as an OpenGL-style normal map. If you’ve imported a material made to be used with another engine, it may be DirectX-style. In this case, the normal map needs to be converted by enabling the Normal Map Invert Y import option.

可以在 这里 找到关于法线贴图(包括常见引擎的坐标顺序表)的更多信息.

Process > HDR as sRGB

Some HDR images you can find online may be broken and contain sRGB color data (instead of linear color data). It is advised not to use those files. If you absolutely have to, enabling this option on will make them look correct.

警告

Enabling HDR as sRGB on well-formatted HDR images will cause the resulting image to look too dark, so leave this disabled if unsure.

Process > HDR Clamp Exposure

Some HDR panorama images you can find online may contain extremely bright pixels, due to being taken from real life sources without any clipping.

While these HDR panorama images are accurate to real life, this can cause the radiance map generated by Godot to contain sparkles when used as a background sky. This can be seen in material reflections (even on rough materials in extreme cases). Enabling HDR Clamp Exposure can resolve this using a smart clamping formula that does not introduce visible clipping – glow will keep working when looking at the background sky.

Process > Size Limit

If set to a value greater than 0, the size of the texture is limited on import to a value smaller than or equal to the value specified here. For non-square textures, the size limit affects the longer dimension, with the shorter dimension scaled to preserve aspect ratio. Resizing is performed using cubic interpolation.

This can be used to reduce memory usage without affecting the source images, or avoid issues with textures not displaying on mobile/web platforms (as these usually can’t display textures larger than 4096×4096).

Detect 3D > Compress To

This changes the 压缩 > 模式 option that is used when a texture is detected as being used in 3D.

Changing this import option only has an effect if a texture is detected as being used in 3D. Changing this to Disabled then reimporting will not change the existing compress mode on a texture (if it’s detected to be used in 3D), but choosing VRAM Compressed or Basis Universal will.

最佳实践

Supporting high-resolution texture sizes in 2D without artifacts

To support multiple resolutions with crisp visuals at high resolutions, you will need to use high-resolution source images (suited for the highest resolution you wish to support without blurriness, which is typically 4K in modern desktop games).

There are 2 ways to proceed:

  • Use a high base resolution in the project settings (such as 4K), then use the textures at original scale. This is an easier approach.

  • Use a low base resolution in the project settings (such as 1080p), then downscale textures when using them. This is often more difficult and can make various calculations in script tedious, so the approach described above is recommended instead.

After doing this, you may notice that textures become grainy at lower viewport resolutions. To resolve this, enable Mipmaps on textures used in 2D in the Import dock. This will increase memory usage.

Enabling mipmaps can also make textures appear blurrier, but you can choose to make textures sharper (at the cost of some graininess) by setting Rendering > Textures > Default Filters > Texture Mipmap Bias to a negative value.

Use appropriate texture sizes in 3D

While there’s no “one size fits all” recommendation, here are some general recommendations for choosing texture sizes in 3D:

  • The size of a texture should be adjusted to have a consistent texel density compared to surrounding objects. While this cannot be ensured perfectly when sticking to power-of-two texture sizes, it’s usually possible to keep texture detail fairly consistent throughout a 3D scene.

  • The smaller the object appears on screen, the smaller its texture should be. For example, a tree that only appears in the background doesn’t need a texture resolution as high as other objects the player may be able to walk close to.

  • Using power-of-two texture sizes is recommended, but is not required. Textures don’t have to be square – sizes such as 1024×512 are acceptable.

  • There are diminishing returns to using large texture sizes, despite the increased memory usage and loading times. Most modern 3D games not using a pixel art style stick to 2048×2048 textures on average, with 1024×1024 and 512×512 for textures spanning smaller surfaces.

  • When working with physically-based materials in 3D, you can reduce memory usage and file size without affecting quality too much by using a lower resolution for certain texture maps. This works especially well for textures that only feature low-frequency detail (such as a normal map for a snow texture).

If you have control over how the 3D models are created, these tips are also worth exploring:

  • When working with 3D models that are mostly symmetrical, you may be able to use mirrored UVs to double the effective texel density. This may look unnatural when used on human faces though.

  • When working with 3D models using a low-poly style and plain colors, you can rely on vertex colors instead of textures to represent colors on the model’s surfaces.

参见

Images can be loaded and saved at runtime using runtime file loading and saving, including from an exported project.

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.