Skip to content

Commit

Permalink
Store/paint colormap in srgb, convert to linear in shader, instead of…
Browse files Browse the repository at this point in the history
… store/paint linear. Upgrade older maps
  • Loading branch information
TokisanGames committed Oct 26, 2023
1 parent 4fc9b4c commit 64dc3e4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
4 changes: 1 addition & 3 deletions project/addons/terrain_3d/editor/components/tool_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ func add_setting(p_type: SettingType, p_name: StringName, value: Variant, parent
func get_setting(p_setting: String) -> Variant:
var object: Object = settings[p_setting]
var value: Variant

if object is Range:
value = object.get_value()
elif object is DoubleSlider:
Expand All @@ -283,8 +282,7 @@ func get_setting(p_setting: String) -> Variant:
elif object is CheckBox:
value = object.is_pressed()
elif object is ColorPickerButton:
value = object.color.srgb_to_linear()

value = object.color
return value


Expand Down
3 changes: 1 addition & 2 deletions project/addons/terrain_3d/editor/components/ui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func update_decal() -> void:
_:
decal.modulate = Color.WHITE
Terrain3DEditor.COLOR:
decal.modulate = brush_data["color"]
decal.modulate = brush_data["color"].srgb_to_linear()*.5
Terrain3DEditor.ROUGHNESS:
decal.modulate = COLOR_ROUGHNESS
_:
Expand All @@ -224,7 +224,6 @@ func update_decal() -> void:
decal_timer.start()



func set_decal_rotation(rot: float) -> void:
decal.rotation.y = rot

Expand Down
1 change: 1 addition & 0 deletions project/addons/terrain_3d/tools/importer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func start_import(value: bool) -> void:
material.show_checkered = false
material.show_colormap = true
storage.import_images(imported_images, import_position, import_offset, import_scale)
print("Import finished")


@export_group("Export File")
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ uniform int _region_map[256];
uniform vec2 _region_offsets[256];
uniform sampler2DArray _height_maps : filter_linear_mipmap, repeat_disable;
uniform sampler2DArray _control_maps : filter_linear_mipmap, repeat_disable;
uniform sampler2DArray _color_maps : filter_linear_mipmap, repeat_disable;
uniform sampler2DArray _color_maps : source_color, filter_linear, repeat_disable;

uniform sampler2DArray _texture_array_albedo : source_color, filter_linear_mipmap_anisotropic, repeat_enable;
uniform sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
Expand Down
1 change: 1 addition & 0 deletions src/terrain_3d_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void Terrain3DMaterial::_update_regions(const Array &p_args) {
}
RS->material_set_param(_material, "_region_map", _region_map);
RS->material_set_param(_material, "_region_map_size", Terrain3DStorage::REGION_MAP_SIZE);
RS->material_set_param(_material, "_region_uv_limit", Terrain3DStorage::REGION_MAP_SIZE / 2);
if (Terrain3D::_debug_level >= DEBUG) {
LOG(DEBUG, "Region map");
for (int i = 0; i < _region_map.size(); i++) {
Expand Down
20 changes: 19 additions & 1 deletion src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,25 @@ void Terrain3DStorage::set_control_maps(const TypedArray<Image> &p_maps) {

void Terrain3DStorage::set_color_maps(const TypedArray<Image> &p_maps) {
LOG(INFO, "Setting color maps: ", p_maps.size());
_color_maps = sanitize_maps(TYPE_COLOR, p_maps);

TypedArray<Image> maps = p_maps;
// DEPRECATED 0.8.4 Remove 0.9
// Convert colormap from linear <0.8.4 to srgb 0.8.41
if (_version < CURRENT_VERSION && maps.size() > 0) {
LOG(WARN, "Color maps are being converted from linear to srgb. Upgrading: ", vformat("%.2f", _version), "->", vformat("%.2f", CURRENT_VERSION));
for (int i = 0; i < maps.size(); i++) {
Ref<Image> img = maps[i];
for (int x = 0; x < img->get_width(); x++) {
for (int y = 0; y < img->get_height(); y++) {
Color c = img->get_pixel(x, y);
img->set_pixel(x, y, c.linear_to_srgb());
}
}
maps[i] = img;
}
_version = CURRENT_VERSION; // Prevent running again on focus
}
_color_maps = sanitize_maps(TYPE_COLOR, maps);
force_update_maps(TYPE_COLOR);
}

Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Terrain3DStorage : public Resource {

// Constants & Definitions

static inline const real_t CURRENT_VERSION = 0.84;
static inline const real_t CURRENT_VERSION = 0.841;
static inline const int REGION_MAP_SIZE = 16;
static inline const Vector2i REGION_MAP_VSIZE = Vector2i(REGION_MAP_SIZE, REGION_MAP_SIZE);

Expand Down

0 comments on commit 64dc3e4

Please sign in to comment.