From c94b99acce848c0850cdc18a1c184c622a9ab956 Mon Sep 17 00:00:00 2001 From: Xtarsia <69606701+Xtarsia@users.noreply.github.com> Date: Sat, 2 Nov 2024 01:39:21 +0000 Subject: [PATCH 1/5] use shader for all brush decals --- project/addons/terrain_3d/src/ui.gd | 241 ++++++++++------------------ src/shaders/editor_functions.glsl | 7 +- src/terrain_3d_material.cpp | 4 +- 3 files changed, 92 insertions(+), 160 deletions(-) diff --git a/project/addons/terrain_3d/src/ui.gd b/project/addons/terrain_3d/src/ui.gd index 0040dcc2..a9ed15c3 100644 --- a/project/addons/terrain_3d/src/ui.gd +++ b/project/addons/terrain_3d/src/ui.gd @@ -41,21 +41,19 @@ var setting_has_changed: bool = false var visible: bool = false var picking: int = Terrain3DEditor.TOOL_MAX var picking_callback: Callable -var decal: Decal -var decal_timer: Timer -var gradient_decals: Array[Decal] var brush_data: Dictionary var operation_builder: OperationBuilder var last_tool: Terrain3DEditor.Tool var last_operation: Terrain3DEditor.Operation var last_rmb_time: int = 0 # Set in editor.gd -# Compatibility decals, indices; 0 = main brush, 1 = slope point A, 2 = slope point B -var editor_decal_position: Array[Vector2] -var editor_decal_rotation: Array[float] -var editor_decal_size: Array[float] -var editor_decal_color: Array[Color] -var editor_decal_visible: Array[bool] +# Editor decals, indices; 0 = main brush, 1 = slope point A, 2 = slope point B +var editor_decal_position: Array[Vector2] = [Vector2(), Vector2(), Vector2()] +var editor_decal_rotation: Array[float] = [float(), float(), float()] +var editor_decal_size: Array[float] = [float(), float(), float()] +var editor_decal_color: Array[Color] = [Color(), Color(), Color()] +var editor_decal_visible: Array[bool] = [bool(), bool(), bool()] +var editor_decal_texture_rid: Array[RID] = [RID(), RID(), RID()] func _enter_tree() -> void: @@ -78,16 +76,6 @@ func _enter_tree() -> void: plugin.add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, terrain_menu) _on_tool_changed(Terrain3DEditor.REGION, Terrain3DEditor.ADD) - - decal = Decal.new() - add_child(decal) - decal_timer = Timer.new() - decal_timer.wait_time = .5 - decal_timer.one_shot = true - decal_timer.timeout.connect(Callable(func(node): - if node: - get_tree().create_tween().tween_property(node, "albedo_mix", 0.0, 0.15)).bind(decal)) - add_child(decal_timer) func _exit_tree() -> void: @@ -96,11 +84,6 @@ func _exit_tree() -> void: toolbar.queue_free() tool_settings.queue_free() terrain_menu.queue_free() - decal.queue_free() - decal_timer.queue_free() - for gradient_decal in gradient_decals: - gradient_decal.queue_free() - gradient_decals.clear() func set_visible(p_visible: bool, p_menu_only: bool = false) -> void: @@ -299,6 +282,10 @@ func _invert_operation(p_operation: Terrain3DEditor.Operation, flags: int = OP_N func update_decal() -> void: + if !plugin.terrain: + return + var mat_rid: RID = plugin.terrain.material.get_material_rid() + # If not a state that should show the decal, hide everything and return if not visible or \ not plugin.terrain or \ @@ -309,182 +296,124 @@ func update_decal() -> void: brush_data.is_empty() or \ plugin.editor.get_tool() == Terrain3DEditor.REGION or \ (plugin._input_mode > 0 and not brush_data["show_cursor_while_painting"]): - decal.visible = false - for gradient_decal in gradient_decals: - gradient_decal.visible = false + editor_decal_visible[0] = false + editor_decal_visible[1] = false + editor_decal_visible[2] = false + RenderingServer.material_set_param(mat_rid, "_editor_decal_visible", editor_decal_visible) return - - decal.position = plugin.mouse_global_position - decal.visible = true - decal.size = Vector3.ONE * maxf(brush_data["size"], .5) + + editor_decal_position[0] = Vector2(plugin.mouse_global_position.x, plugin.mouse_global_position.z) + editor_decal_visible[0] = true + editor_decal_size[0] = maxf(brush_data["size"], .5) if brush_data["align_to_view"]: var cam: Camera3D = plugin.terrain.get_camera(); if (cam): - decal.rotation.y = cam.rotation.y + editor_decal_rotation[0] = cam.rotation.y else: - decal.rotation.y = 0 - + editor_decal_rotation[0] = 0 + # Set texture and color if picking != Terrain3DEditor.TOOL_MAX: - decal.texture_albedo = ring_texture - decal.size = Vector3.ONE * 10. * plugin.terrain.get_vertex_spacing() + editor_decal_texture_rid[0] = ring_texture.get_rid() + editor_decal_size[0] = 10. * plugin.terrain.get_vertex_spacing() match picking: Terrain3DEditor.HEIGHT: - decal.modulate = COLOR_PICK_HEIGHT + editor_decal_color[0] = COLOR_PICK_HEIGHT Terrain3DEditor.COLOR: - decal.modulate = COLOR_PICK_COLOR + editor_decal_color[0] = COLOR_PICK_COLOR Terrain3DEditor.ROUGHNESS: - decal.modulate = COLOR_PICK_ROUGH - decal.modulate.a = 1.0 + editor_decal_color[0] = COLOR_PICK_ROUGH + editor_decal_color[0].a = 1.0 else: - decal.texture_albedo = brush_data["brush"][1] + editor_decal_texture_rid[0] = brush_data["brush"][1].get_rid() match plugin.editor.get_tool(): Terrain3DEditor.SCULPT: match plugin.editor.get_operation(): Terrain3DEditor.ADD: if plugin.modifier_alt: - decal.modulate = COLOR_LIFT - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_LIFT + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) else: - decal.modulate = COLOR_RAISE - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_RAISE + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) Terrain3DEditor.SUBTRACT: if plugin.modifier_alt: - decal.modulate = COLOR_FLATTEN - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_FLATTEN + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) else: - decal.modulate = COLOR_LOWER - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + .5 + editor_decal_color[0] = COLOR_LOWER + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) + .5 Terrain3DEditor.AVERAGE: - decal.modulate = COLOR_SMOOTH - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + .2 + editor_decal_color[0] = COLOR_SMOOTH + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) + .2 Terrain3DEditor.GRADIENT: - decal.modulate = COLOR_SLOPE - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_SLOPE + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) Terrain3DEditor.HEIGHT: - decal.modulate = COLOR_HEIGHT - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_HEIGHT + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) Terrain3DEditor.TEXTURE: match plugin.editor.get_operation(): Terrain3DEditor.REPLACE: - decal.modulate = COLOR_PAINT - decal.modulate.a = .7 + editor_decal_color[0] = COLOR_PAINT + editor_decal_color[0].a = .7 Terrain3DEditor.ADD: - decal.modulate = COLOR_SPRAY - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_SPRAY + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) Terrain3DEditor.COLOR: - decal.modulate = brush_data["color"].srgb_to_linear() - decal.modulate.a *= clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = brush_data["color"].srgb_to_linear() + editor_decal_color[0].a *= clamp(brush_data["strength"], .2, .5) Terrain3DEditor.ROUGHNESS: - decal.modulate = COLOR_ROUGHNESS - decal.modulate.a = clamp(brush_data["strength"], .2, .5) + editor_decal_color[0] = COLOR_ROUGHNESS + editor_decal_color[0].a = clamp(brush_data["strength"], .2, .5) Terrain3DEditor.AUTOSHADER: - decal.modulate = COLOR_AUTOSHADER - decal.modulate.a = .7 + editor_decal_color[0] = COLOR_AUTOSHADER + editor_decal_color[0].a = .7 Terrain3DEditor.HOLES: - decal.modulate = COLOR_HOLES - decal.modulate.a = .85 + editor_decal_color[0] = COLOR_HOLES + editor_decal_color[0].a = .85 Terrain3DEditor.NAVIGATION: - decal.modulate = COLOR_NAVIGATION - decal.modulate.a = .85 + editor_decal_color[0] = COLOR_NAVIGATION + editor_decal_color[0].a = .85 Terrain3DEditor.INSTANCER: - decal.texture_albedo = ring_texture - decal.modulate = COLOR_INSTANCER - decal.modulate.a = 1.0 - decal.size.y = max(1000, decal.size.y) - decal.albedo_mix = 1.0 - decal.cull_mask = 1 << ( plugin.terrain.get_mouse_layer() - 1 ) - decal_timer.start() - - for gradient_decal in gradient_decals: - gradient_decal.visible = false + editor_decal_texture_rid[0] = ring_texture.get_rid() + editor_decal_color[0] = COLOR_INSTANCER + editor_decal_color[0].a = 1.0 - if plugin.editor.get_operation() == Terrain3DEditor.GRADIENT: - var index := 0 - for point in brush_data["gradient_points"]: - if point != Vector3.ZERO: - var point_decal: Decal = _get_gradient_decal(index) - point_decal.visible = true - point_decal.position = point - index += 1 - - update_compatibility_decal() - - -func _get_gradient_decal(index: int) -> Decal: - if gradient_decals.size() > index: - return gradient_decals[index] + editor_decal_visible[1] = false + editor_decal_visible[2] = false - var gradient_decal := Decal.new() - gradient_decal = Decal.new() - gradient_decal.texture_albedo = ring_texture - gradient_decal.modulate = COLOR_SLOPE - gradient_decal.size = Vector3.ONE * 10. * plugin.terrain.get_vertex_spacing() - gradient_decal.size.y = 1000. - gradient_decal.cull_mask = decal.cull_mask - add_child(gradient_decal) + if plugin.editor.get_operation() == Terrain3DEditor.GRADIENT: + var point1: Vector3 = brush_data["gradient_points"][0] + if point1 != Vector3.ZERO: + editor_decal_texture_rid[1] = ring_texture.get_rid() + editor_decal_color[1] = COLOR_SLOPE + editor_decal_size[1] = 10. * plugin.terrain.get_vertex_spacing() + editor_decal_visible[1] = true + editor_decal_position[1] = Vector2(point1.x, point1.z) + var point2: Vector3 = brush_data["gradient_points"][1] + if point2 != Vector3.ZERO: + editor_decal_texture_rid[2] = ring_texture.get_rid() + editor_decal_color[2] = COLOR_SLOPE + editor_decal_size[2] = 10. * plugin.terrain.get_vertex_spacing() + editor_decal_visible[2] = true + editor_decal_position[2] = Vector2(point2.x, point2.z) - gradient_decals.push_back(gradient_decal) - return gradient_decal - - -func update_compatibility_decal() -> void: - if not plugin.terrain.is_compatibility_mode(): - return - - # Verify setup - if editor_decal_position.size() != 3: - editor_decal_position.resize(3) - editor_decal_rotation.resize(3) - editor_decal_size.resize(3) - editor_decal_color.resize(3) - editor_decal_visible.resize(3) - decal_timer.timeout.connect(func(): - var mat_rid: RID = plugin.terrain.material.get_material_rid() - editor_decal_visible[0] = false - RenderingServer.material_set_param(mat_rid, "_editor_decal_visible", editor_decal_visible) - ) - - # Update compatibility decal - var mat_rid: RID = plugin.terrain.material.get_material_rid() - if decal.visible: - editor_decal_position[0] = Vector2(decal.global_position.x, decal.global_position.z) - editor_decal_rotation[0] = decal.rotation.y - editor_decal_size[0] = brush_data.get("size") - editor_decal_color[0] = decal.modulate - editor_decal_visible[0] = decal.visible - RenderingServer.material_set_param( - mat_rid, "_editor_decal_0", decal.texture_albedo.get_rid() - ) - if gradient_decals.size() >= 1: - editor_decal_position[1] = Vector2(gradient_decals[0].global_position.x, - gradient_decals[0].global_position.z) - editor_decal_rotation[1] = gradient_decals[0].rotation.y - editor_decal_size[1] = 10.0 - editor_decal_color[1] = gradient_decals[0].modulate - editor_decal_visible[1] = gradient_decals[0].visible - RenderingServer.material_set_param( - mat_rid, "_editor_decal_1", gradient_decals[0].texture_albedo.get_rid() - ) - if gradient_decals.size() >= 2: - editor_decal_position[2] = Vector2(gradient_decals[1].global_position.x, - gradient_decals[1].global_position.z) - editor_decal_rotation[2] = gradient_decals[1].rotation.y - editor_decal_size[2] = 10.0 - editor_decal_color[2] = gradient_decals[1].modulate - editor_decal_visible[2] = gradient_decals[1].visible - RenderingServer.material_set_param( - mat_rid, "_editor_decal_2", gradient_decals[1].texture_albedo.get_rid() - ) + # Update Shader params + RenderingServer.material_set_param(mat_rid, "_editor_decal_0", editor_decal_texture_rid[0]) + RenderingServer.material_set_param(mat_rid, "_editor_decal_1", editor_decal_texture_rid[1]) + RenderingServer.material_set_param(mat_rid, "_editor_decal_2", editor_decal_texture_rid[2]) RenderingServer.material_set_param(mat_rid, "_editor_decal_position", editor_decal_position) RenderingServer.material_set_param(mat_rid, "_editor_decal_rotation", editor_decal_rotation) RenderingServer.material_set_param(mat_rid, "_editor_decal_size", editor_decal_size) RenderingServer.material_set_param(mat_rid, "_editor_decal_color", editor_decal_color) RenderingServer.material_set_param(mat_rid, "_editor_decal_visible", editor_decal_visible) - + RenderingServer.material_set_param(mat_rid, "_editor_decal_timestamp", float(Time.get_ticks_msec()) / 1000.0 + 0.5) func set_decal_rotation(p_rot: float) -> void: - decal.rotation.y = p_rot + editor_decal_rotation[0] = p_rot + var mat_rid: RID = plugin.terrain.material.get_material_rid() + RenderingServer.material_set_param(mat_rid, "_editor_decal_rotation", editor_decal_rotation) func _on_picking(p_type: int, p_callback: Callable) -> void: diff --git a/src/shaders/editor_functions.glsl b/src/shaders/editor_functions.glsl index ae851447..22b987fd 100644 --- a/src/shaders/editor_functions.glsl +++ b/src/shaders/editor_functions.glsl @@ -37,6 +37,7 @@ uniform float _editor_decal_size[3]; uniform float _editor_decal_rotation[3]; uniform vec4 _editor_decal_color[3] : source_color; uniform bool _editor_decal_visible[3]; +uniform float _editor_decal_timestamp; // expects uv (Texture/world space 0 to +/- inf 1m units). vec3 get_decal(vec3 albedo, vec2 uv) { @@ -67,8 +68,10 @@ vec3 get_decal(vec3 albedo, vec2 uv) { decal = texture(_editor_decal_2, decal_uv + 0.5).r; break; } - // Blend in decal; reduce opacity 55% to account for differences in Opengl/Vulkan and/or decals - albedo = mix(albedo, _editor_decal_color[i].rgb, clamp(decal * _editor_decal_color[i].a * .55, 0., .55)); + // Decal fadeout from last time stamp + float fade = clamp((_editor_decal_timestamp - TIME) * 8. - 6., 0., 1.); + // Blend in decal; square for better visual blend + albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a * fade); } return albedo; diff --git a/src/terrain_3d_material.cpp b/src/terrain_3d_material.cpp index 702dc1b0..c0ad3316 100644 --- a/src/terrain_3d_material.cpp +++ b/src/terrain_3d_material.cpp @@ -183,7 +183,7 @@ String Terrain3DMaterial::_inject_editor_code(const String &p_shader) const { return shader; } insert_names.clear(); - if (_compatibility) { + if (IS_EDITOR && _terrain && _terrain->get_editor()) { insert_names.push_back("EDITOR_SETUP_DECAL"); } for (int i = 0; i < insert_names.size(); i++) { @@ -250,7 +250,7 @@ String Terrain3DMaterial::_inject_editor_code(const String &p_shader) const { if (_show_navigation || (IS_EDITOR && _terrain && _terrain->get_editor() && _terrain->get_editor()->get_tool() == Terrain3DEditor::NAVIGATION)) { insert_names.push_back("EDITOR_NAVIGATION"); } - if (_compatibility) { + if (IS_EDITOR && _terrain && _terrain->get_editor()) { insert_names.push_back("EDITOR_RENDER_DECAL"); } for (int i = 0; i < insert_names.size(); i++) { From fdf39080cb9fb96b869f5fc37af1cf16e257ea95 Mon Sep 17 00:00:00 2001 From: Xtarsia <69606701+Xtarsia@users.noreply.github.com> Date: Sat, 2 Nov 2024 12:36:22 +0000 Subject: [PATCH 2/5] remove 3rd sampler uniform --- project/addons/terrain_3d/src/ui.gd | 17 +++++++---------- src/shaders/editor_functions.glsl | 17 ++++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/project/addons/terrain_3d/src/ui.gd b/project/addons/terrain_3d/src/ui.gd index a9ed15c3..d182ceeb 100644 --- a/project/addons/terrain_3d/src/ui.gd +++ b/project/addons/terrain_3d/src/ui.gd @@ -53,7 +53,8 @@ var editor_decal_rotation: Array[float] = [float(), float(), float()] var editor_decal_size: Array[float] = [float(), float(), float()] var editor_decal_color: Array[Color] = [Color(), Color(), Color()] var editor_decal_visible: Array[bool] = [bool(), bool(), bool()] -var editor_decal_texture_rid: Array[RID] = [RID(), RID(), RID()] +var editor_brush_texture_rid: RID = RID() +@onready var editor_ring_texture_rid: RID = ring_texture.get_rid() func _enter_tree() -> void: @@ -241,7 +242,6 @@ func _on_setting_changed() -> void: return brush_data = tool_settings.get_settings() brush_data["asset_id"] = plugin.asset_dock.get_current_list().get_selected_id() - update_decal() plugin.editor.set_brush_data(brush_data) plugin.editor.set_operation(_modify_operation(plugin.editor.get_operation())) @@ -314,7 +314,7 @@ func update_decal() -> void: # Set texture and color if picking != Terrain3DEditor.TOOL_MAX: - editor_decal_texture_rid[0] = ring_texture.get_rid() + editor_brush_texture_rid = ring_texture.get_rid() editor_decal_size[0] = 10. * plugin.terrain.get_vertex_spacing() match picking: Terrain3DEditor.HEIGHT: @@ -325,7 +325,7 @@ func update_decal() -> void: editor_decal_color[0] = COLOR_PICK_ROUGH editor_decal_color[0].a = 1.0 else: - editor_decal_texture_rid[0] = brush_data["brush"][1].get_rid() + editor_brush_texture_rid = brush_data["brush"][1].get_rid() match plugin.editor.get_tool(): Terrain3DEditor.SCULPT: match plugin.editor.get_operation(): @@ -376,7 +376,7 @@ func update_decal() -> void: editor_decal_color[0] = COLOR_NAVIGATION editor_decal_color[0].a = .85 Terrain3DEditor.INSTANCER: - editor_decal_texture_rid[0] = ring_texture.get_rid() + editor_brush_texture_rid = ring_texture.get_rid() editor_decal_color[0] = COLOR_INSTANCER editor_decal_color[0].a = 1.0 @@ -386,23 +386,20 @@ func update_decal() -> void: if plugin.editor.get_operation() == Terrain3DEditor.GRADIENT: var point1: Vector3 = brush_data["gradient_points"][0] if point1 != Vector3.ZERO: - editor_decal_texture_rid[1] = ring_texture.get_rid() editor_decal_color[1] = COLOR_SLOPE editor_decal_size[1] = 10. * plugin.terrain.get_vertex_spacing() editor_decal_visible[1] = true editor_decal_position[1] = Vector2(point1.x, point1.z) var point2: Vector3 = brush_data["gradient_points"][1] if point2 != Vector3.ZERO: - editor_decal_texture_rid[2] = ring_texture.get_rid() editor_decal_color[2] = COLOR_SLOPE editor_decal_size[2] = 10. * plugin.terrain.get_vertex_spacing() editor_decal_visible[2] = true editor_decal_position[2] = Vector2(point2.x, point2.z) # Update Shader params - RenderingServer.material_set_param(mat_rid, "_editor_decal_0", editor_decal_texture_rid[0]) - RenderingServer.material_set_param(mat_rid, "_editor_decal_1", editor_decal_texture_rid[1]) - RenderingServer.material_set_param(mat_rid, "_editor_decal_2", editor_decal_texture_rid[2]) + RenderingServer.material_set_param(mat_rid, "_editor_brush_texture", editor_brush_texture_rid) + RenderingServer.material_set_param(mat_rid, "_editor_ring_texture", editor_ring_texture_rid) RenderingServer.material_set_param(mat_rid, "_editor_decal_position", editor_decal_position) RenderingServer.material_set_param(mat_rid, "_editor_decal_rotation", editor_decal_rotation) RenderingServer.material_set_param(mat_rid, "_editor_decal_size", editor_decal_size) diff --git a/src/shaders/editor_functions.glsl b/src/shaders/editor_functions.glsl index 22b987fd..70d69560 100644 --- a/src/shaders/editor_functions.glsl +++ b/src/shaders/editor_functions.glsl @@ -29,9 +29,8 @@ R"( // END_COMPAT_DEFINES //INSERT: EDITOR_SETUP_DECAL -uniform highp sampler2D _editor_decal_0 : source_color, filter_linear, repeat_disable; -uniform highp sampler2D _editor_decal_1 : source_color, filter_linear, repeat_disable; -uniform highp sampler2D _editor_decal_2 : source_color, filter_linear, repeat_disable; +uniform highp sampler2D _editor_brush_texture : source_color, filter_linear, repeat_disable; +uniform highp sampler2D _editor_ring_texture : source_color, filter_linear, repeat_disable; uniform vec2 _editor_decal_position[3]; uniform float _editor_decal_size[3]; uniform float _editor_decal_rotation[3]; @@ -41,6 +40,8 @@ uniform float _editor_decal_timestamp; // expects uv (Texture/world space 0 to +/- inf 1m units). vec3 get_decal(vec3 albedo, vec2 uv) { + // Decal fadeout from last time stamp + float fade = clamp((_editor_decal_timestamp - TIME) * 8. - 6., 0., 1.); for (int i = 0; i < 3; ++i) { if (!_editor_decal_visible[i]) { continue; @@ -55,21 +56,15 @@ vec3 get_decal(vec3 albedo, vec2 uv) { continue; } float decal = 0.0; - // For webGL we cannot use sampler2D[], sampler2DArray requires all textures be the same size, - // which might not be the case - so use a switch to read the correct uniform. switch (i) { case 0 : - decal = texture(_editor_decal_0, decal_uv + 0.5).r; + decal = texture(_editor_brush_texture, decal_uv + 0.5).r; break; case 1: - decal = texture(_editor_decal_1, decal_uv + 0.5).r; - break; case 2: - decal = texture(_editor_decal_2, decal_uv + 0.5).r; + decal = texture(_editor_ring_texture, decal_uv + 0.5).r; break; } - // Decal fadeout from last time stamp - float fade = clamp((_editor_decal_timestamp - TIME) * 8. - 6., 0., 1.); // Blend in decal; square for better visual blend albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a * fade); } From d1f2289caac65cc117332bda5588499ce6f6eeea Mon Sep 17 00:00:00 2001 From: Xtarsia <69606701+Xtarsia@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:09:05 +0000 Subject: [PATCH 3/5] add crosshair --- project/addons/terrain_3d/src/tool_settings.gd | 2 ++ project/addons/terrain_3d/src/ui.gd | 2 ++ src/shaders/editor_functions.glsl | 12 +++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/project/addons/terrain_3d/src/tool_settings.gd b/project/addons/terrain_3d/src/tool_settings.gd index dd14104f..848c61d6 100644 --- a/project/addons/terrain_3d/src/tool_settings.gd +++ b/project/addons/terrain_3d/src/tool_settings.gd @@ -171,6 +171,8 @@ func _ready() -> void: "unit":"γ", "range":Vector3(0.1, 2.0, 0.01) }) add_setting({ "name":"jitter", "type":SettingType.SLIDER, "list":advanced_list, "default":50, "unit":"%", "range":Vector3(0, 100, 1) }) + add_setting({ "name":"cross_hair_threshold", "type":SettingType.SLIDER, "list":advanced_list, "default":16., + "unit":"m", "range":Vector3(0, 200, 1) }) func create_submenu(p_parent: Control, p_button_name: String, p_layout: Layout, p_hover_pop: bool = true) -> Container: diff --git a/project/addons/terrain_3d/src/ui.gd b/project/addons/terrain_3d/src/ui.gd index d182ceeb..b3d1a069 100644 --- a/project/addons/terrain_3d/src/ui.gd +++ b/project/addons/terrain_3d/src/ui.gd @@ -220,6 +220,7 @@ func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor to_show.push_back("show_cursor_while_painting") to_show.push_back("gamma") to_show.push_back("jitter") + to_show.push_back("cross_hair_threshold") tool_settings.show_settings(to_show) operation_builder = null @@ -406,6 +407,7 @@ func update_decal() -> void: RenderingServer.material_set_param(mat_rid, "_editor_decal_color", editor_decal_color) RenderingServer.material_set_param(mat_rid, "_editor_decal_visible", editor_decal_visible) RenderingServer.material_set_param(mat_rid, "_editor_decal_timestamp", float(Time.get_ticks_msec()) / 1000.0 + 0.5) + RenderingServer.material_set_param(mat_rid, "_editor_cross_hair_threshold", brush_data["cross_hair_threshold"] + 0.1) func set_decal_rotation(p_rot: float) -> void: editor_decal_rotation[0] = p_rot diff --git a/src/shaders/editor_functions.glsl b/src/shaders/editor_functions.glsl index 70d69560..b10ff5e3 100644 --- a/src/shaders/editor_functions.glsl +++ b/src/shaders/editor_functions.glsl @@ -37,6 +37,7 @@ uniform float _editor_decal_rotation[3]; uniform vec4 _editor_decal_color[3] : source_color; uniform bool _editor_decal_visible[3]; uniform float _editor_decal_timestamp; +uniform float _editor_cross_hair_threshold; // expects uv (Texture/world space 0 to +/- inf 1m units). vec3 get_decal(vec3 albedo, vec2 uv) { @@ -68,7 +69,16 @@ vec3 get_decal(vec3 albedo, vec2 uv) { // Blend in decal; square for better visual blend albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a * fade); } - + if (_editor_decal_visible[0] && _editor_decal_size[0] <= _editor_cross_hair_threshold) { + vec2 cross_uv = ((uv - _editor_decal_position[0] * _vertex_density) * _vertex_spacing); + float line_thickness = 0.05 * sqrt(length(v_camera_pos - v_vertex)); + float line_start = _editor_decal_size[0] + 8.; + // Scale length as size increases + float line_end = _editor_decal_size[0] + 16. + mix(0., 48., _editor_decal_size[0] * 0.01); + bool h = abs(cross_uv.y) < line_thickness && abs(cross_uv.x) < line_end && abs(cross_uv.x) > line_start; + bool v = abs(cross_uv.x) < line_thickness && abs(cross_uv.y) < line_end && abs(cross_uv.y) > line_start; + albedo = (h || v) ? mix(albedo, _editor_decal_color[0].rgb, fade) : albedo; + } return albedo; } From cc383ab6f8c0ae69cb9d961d530f8a9614e680c7 Mon Sep 17 00:00:00 2001 From: Xtarsia <69606701+Xtarsia@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:59:16 +0000 Subject: [PATCH 4/5] improove cross hair size behaviour --- src/shaders/editor_functions.glsl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/shaders/editor_functions.glsl b/src/shaders/editor_functions.glsl index b10ff5e3..36707da3 100644 --- a/src/shaders/editor_functions.glsl +++ b/src/shaders/editor_functions.glsl @@ -70,11 +70,11 @@ vec3 get_decal(vec3 albedo, vec2 uv) { albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a * fade); } if (_editor_decal_visible[0] && _editor_decal_size[0] <= _editor_cross_hair_threshold) { - vec2 cross_uv = ((uv - _editor_decal_position[0] * _vertex_density) * _vertex_spacing); - float line_thickness = 0.05 * sqrt(length(v_camera_pos - v_vertex)); - float line_start = _editor_decal_size[0] + 8.; - // Scale length as size increases - float line_end = _editor_decal_size[0] + 16. + mix(0., 48., _editor_decal_size[0] * 0.01); + vec2 cross_uv = ((uv - _editor_decal_position[0] * _vertex_density) * _vertex_spacing) * 16.0; + cross_uv /= sqrt(length(v_camera_pos - v_vertex)); + float line_thickness = 0.5; + float line_start = _editor_decal_size[0] * 0.5 + 8.; + float line_end = _editor_decal_size[0] * 0.5 + 32.; bool h = abs(cross_uv.y) < line_thickness && abs(cross_uv.x) < line_end && abs(cross_uv.x) > line_start; bool v = abs(cross_uv.x) < line_thickness && abs(cross_uv.y) < line_end && abs(cross_uv.y) > line_start; albedo = (h || v) ? mix(albedo, _editor_decal_color[0].rgb, fade) : albedo; From 66bcc1553051a6a021904be5adcffeb3f83583d9 Mon Sep 17 00:00:00 2001 From: Xtarsia <69606701+Xtarsia@users.noreply.github.com> Date: Sat, 2 Nov 2024 14:09:59 +0000 Subject: [PATCH 5/5] reduce cross line length --- src/shaders/editor_functions.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shaders/editor_functions.glsl b/src/shaders/editor_functions.glsl index 36707da3..665aaed6 100644 --- a/src/shaders/editor_functions.glsl +++ b/src/shaders/editor_functions.glsl @@ -74,7 +74,7 @@ vec3 get_decal(vec3 albedo, vec2 uv) { cross_uv /= sqrt(length(v_camera_pos - v_vertex)); float line_thickness = 0.5; float line_start = _editor_decal_size[0] * 0.5 + 8.; - float line_end = _editor_decal_size[0] * 0.5 + 32.; + float line_end = _editor_decal_size[0] * 0.5 + 16.; bool h = abs(cross_uv.y) < line_thickness && abs(cross_uv.x) < line_end && abs(cross_uv.x) > line_start; bool v = abs(cross_uv.x) < line_thickness && abs(cross_uv.y) < line_end && abs(cross_uv.y) > line_start; albedo = (h || v) ? mix(albedo, _editor_decal_color[0].rgb, fade) : albedo;