| 94 | } |
| 95 | |
| 96 | void GradientTexture1D::_update() const { |
| 97 | update_pending = false; |
| 98 | |
| 99 | if (gradient.is_null()) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (use_hdr) { |
| 104 | // High dynamic range. |
| 105 | Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF)); |
| 106 | Gradient &g = **gradient; |
| 107 | // `create()` isn't available for non-uint8_t data, so fill in the data manually. |
| 108 | for (int i = 0; i < width; i++) { |
| 109 | float ofs = float(i) / (width - 1); |
| 110 | image->set_pixel(i, 0, g.get_color_at_offset(ofs)); |
| 111 | } |
| 112 | |
| 113 | if (texture.is_valid()) { |
| 114 | RID new_texture = RS::get_singleton()->texture_2d_create(image); |
| 115 | RS::get_singleton()->texture_replace(texture, new_texture); |
| 116 | } else { |
| 117 | texture = RS::get_singleton()->texture_2d_create(image); |
| 118 | } |
| 119 | } else { |
| 120 | // Low dynamic range. "Overbright" colors will be clamped. |
| 121 | Vector<uint8_t> data; |
| 122 | data.resize(width * 4); |
| 123 | { |
| 124 | uint8_t *wd8 = data.ptrw(); |
| 125 | Gradient &g = **gradient; |
| 126 | |
| 127 | for (int i = 0; i < width; i++) { |
| 128 | float ofs = float(i) / (width - 1); |
| 129 | Color color = g.get_color_at_offset(ofs); |
| 130 | |
| 131 | wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255)); |
| 132 | wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255)); |
| 133 | wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255)); |
| 134 | wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data)); |
| 139 | |
| 140 | if (texture.is_valid()) { |
| 141 | RID new_texture = RS::get_singleton()->texture_2d_create(image); |
| 142 | RS::get_singleton()->texture_replace(texture, new_texture); |
| 143 | } else { |
| 144 | texture = RS::get_singleton()->texture_2d_create(image); |
| 145 | } |
| 146 | } |
| 147 | RS::get_singleton()->texture_set_path(texture, get_path()); |
| 148 | } |
| 149 | |
| 150 | void GradientTexture1D::set_width(int p_width) { |
| 151 | ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range."); |
nothing calls this directly
no test coverage detected