Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Gradient

继承: Resource < RefCounted < Object

颜色过渡。

描述

该资源通过定义一组彩色点以及如何在它们之间进行插值来描述颜色过渡。

另见 Curve,它支持更复杂的缓动方法,但不支持颜色。

属性

PackedColorArray

colors

PackedColorArray(0, 0, 0, 1, 1, 1, 1, 1)

ColorSpace

interpolation_color_space

0

InterpolationMode

interpolation_mode

0

PackedFloat32Array

offsets

PackedFloat32Array(0, 1)

方法

void

add_point ( float offset, Color color )

Color

get_color ( int point )

float

get_offset ( int point )

int

get_point_count ( ) const

void

remove_point ( int point )

void

reverse ( )

Color

sample ( float offset )

void

set_color ( int point, Color color )

void

set_offset ( int point, float offset )


枚举

enum InterpolationMode:

InterpolationMode GRADIENT_INTERPOLATE_LINEAR = 0

线性插值。

InterpolationMode GRADIENT_INTERPOLATE_CONSTANT = 1

常量插值,颜色会在每个点上突变,在点和点之间保持一致。在某些情况下用于渐变纹理时,可能会造成明显的锯齿。

InterpolationMode GRADIENT_INTERPOLATE_CUBIC = 2

三次插值。


enum ColorSpace:

ColorSpace GRADIENT_COLOR_SPACE_SRGB = 0

sRGB 色彩空间。

ColorSpace GRADIENT_COLOR_SPACE_LINEAR_SRGB = 1

线性 sRGB 色彩空间。

ColorSpace GRADIENT_COLOR_SPACE_OKLAB = 2

Oklab 色彩空间。该色彩空间能够在不同颜色之间进行平滑、外观统一的过渡。


属性说明

PackedColorArray colors = PackedColorArray(0, 0, 0, 1, 1, 1, 1, 1)

PackedColorArray 形式的渐变色颜色。

注意:这个属性返回的是副本,修改返回值并不会对渐变色进行更新。要更新渐变色,请使用 set_color 方法(单独更新颜色)或直接为这个属性赋值(一次性更新所有颜色)。


ColorSpace interpolation_color_space = 0

  • void set_interpolation_color_space ( ColorSpace value )

  • ColorSpace get_interpolation_color_space ( )

用于在渐变点之间进行插值的色彩空间。不影响返回的颜色,返回的颜色始终在 sRGB 空间。可用的模式见 ColorSpace

注意:interpolation_modeGRADIENT_INTERPOLATE_CONSTANT 时该设置无效。


InterpolationMode interpolation_mode = 0

用于在渐变点之间进行插值的算法。可用的模式见 InterpolationMode


PackedFloat32Array offsets = PackedFloat32Array(0, 1)

PackedFloat32Array 形式的渐变色偏移。

注意:这个属性返回的是副本,修改返回值并不会对渐变色进行更新。要更新渐变色,请使用 set_offset 方法(单独更新偏移)或直接为这个属性赋值(一次性更新所有偏移)。


方法说明

void add_point ( float offset, Color color )

使用指定的偏移,将指定的颜色添加到该渐变中。


Color get_color ( int point )

返回渐变色在索引 point 处的颜色。


float get_offset ( int point )

返回渐变色在索引 point 处的偏移。


int get_point_count ( ) const

返回渐变中的颜色数。


void remove_point ( int point )

移除索引 point 处的颜色。


void reverse ( )

将渐变进行翻转/镜像。

注意:这个方法会将所有点以渐变的中点进行镜像,interpolation_modeGRADIENT_INTERPOLATE_CONSTANT 时可能产生意外的结果。


Color sample ( float offset )

返回由偏移 offset 指定的插值颜色。


void set_color ( int point, Color color )

设置渐变色在索引 point 处的颜色。


void set_offset ( int point, float offset )

设置渐变色在索引 point 处的偏移。