Skip to content

Commit

Permalink
fix timer compatibility colors and crosshair naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtarsia committed Jan 3, 2025
1 parent be83b7f commit 1425924
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 0 additions & 2 deletions project/addons/terrain_3d/src/editor_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ func _forward_3d_gui_input(p_viewport_camera: Camera3D, p_event: InputEvent) ->

return AFTER_GUI_INPUT_PASS

ui.update_decal()

if p_event is InputEventMouseButton and _input_mode > 0:
if p_event.is_pressed():
# If picking
Expand Down
2 changes: 1 addition & 1 deletion project/addons/terrain_3d/src/tool_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ 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.,
add_setting({ "name":"crosshair_threshold", "type":SettingType.SLIDER, "list":advanced_list, "default":16.,
"unit":"m", "range":Vector3(0, 200, 1) })


Expand Down
41 changes: 32 additions & 9 deletions project/addons/terrain_3d/src/ui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,24 @@ var last_operation: Terrain3DEditor.Operation
var last_rmb_time: int = 0 # Set in editor.gd

# Editor decals, indices; 0 = main brush, 1 = slope point A, 2 = slope point B
var mat_rid: RID
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_brush_texture_rid: RID = RID()
var editor_decal_timer: Timer
var editor_decal_fade: float :
set(value):
editor_decal_fade = value
editor_decal_color[0] = Color(
editor_decal_color[0].r,
editor_decal_color[0].g,
editor_decal_color[0].b,
value
)
RenderingServer.material_set_param(mat_rid, "_editor_decal_color", editor_decal_color)
@onready var editor_ring_texture_rid: RID = ring_texture.get_rid()


Expand All @@ -78,6 +90,13 @@ func _enter_tree() -> void:
plugin.add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, terrain_menu)

_on_tool_changed(Terrain3DEditor.REGION, Terrain3DEditor.ADD)

editor_decal_timer = Timer.new()
editor_decal_timer.wait_time = .5
editor_decal_timer.one_shot = true
editor_decal_timer.timeout.connect(func():
get_tree().create_tween().tween_property(self, "editor_decal_fade", 0.0, 0.15))
add_child(editor_decal_timer)


func _exit_tree() -> void:
Expand All @@ -86,7 +105,7 @@ func _exit_tree() -> void:
toolbar.queue_free()
tool_settings.queue_free()
terrain_menu.queue_free()

editor_decal_timer.queue_free()

func set_visible(p_visible: bool, p_menu_only: bool = false) -> void:
terrain_menu.set_visible(p_visible)
Expand Down Expand Up @@ -221,7 +240,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")
to_show.push_back("crosshair_threshold")
tool_settings.show_settings(to_show)

operation_builder = null
Expand All @@ -246,7 +265,7 @@ func _on_setting_changed() -> void:
brush_data["asset_id"] = plugin.asset_dock.get_current_list().get_selected_id()
plugin.editor.set_brush_data(brush_data)
plugin.editor.set_operation(_modify_operation(plugin.editor.get_operation()))

update_decal()

func update_modifiers() -> void:
toolbar.show_add_buttons(not plugin.modifier_ctrl)
Expand Down Expand Up @@ -284,13 +303,13 @@ func _invert_operation(p_operation: Terrain3DEditor.Operation, flags: int = OP_N


func update_decal() -> void:
if !plugin.terrain:
if not plugin.terrain:
return
var mat_rid: RID = plugin.terrain.material.get_material_rid()
mat_rid = plugin.terrain.material.get_material_rid()
editor_decal_timer.start()

# If not a state that should show the decal, hide everything and return
if not visible or \
not plugin.terrain or \
plugin._input_mode < 0 or \
# Wait for cursor to recenter after moving camera before revealing
# See https://github.com/godotengine/godot/issues/70098
Expand All @@ -312,7 +331,7 @@ func update_decal() -> void:
if (cam):
editor_decal_rotation[0] = cam.rotation.y
else:
editor_decal_rotation[0] = 0
editor_decal_rotation[0] = 0.

# Set texture and color
if picking != Terrain3DEditor.TOOL_MAX:
Expand Down Expand Up @@ -402,6 +421,11 @@ func update_decal() -> void:
editor_decal_visible[2] = true
editor_decal_position[2] = Vector2(point2.x, point2.z)

if plugin.terrain.is_compatibility_mode():
for i in editor_decal_color.size():
editor_decal_color[i] = editor_decal_color[i].darkened(0.2)

editor_decal_fade = editor_decal_color[0].a
# Update Shader params
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)
Expand All @@ -410,8 +434,7 @@ func update_decal() -> void:
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)
RenderingServer.material_set_param(mat_rid, "_editor_cross_hair_threshold", brush_data["cross_hair_threshold"] + 0.1)
RenderingServer.material_set_param(mat_rid, "_editor_crosshair_threshold", brush_data["crosshair_threshold"] + 0.1)

func set_decal_rotation(p_rot: float) -> void:
editor_decal_rotation[0] = p_rot
Expand Down
12 changes: 5 additions & 7 deletions src/shaders/editor_functions.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ 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;
uniform float _editor_cross_hair_threshold;
uniform float _editor_crosshair_threshold = 16.0;

// 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;
Expand All @@ -67,17 +64,18 @@ vec3 get_decal(vec3 albedo, vec2 uv) {
break;
}
// Blend in decal; square for better visual blend
albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a * fade);
albedo = mix(albedo, _editor_decal_color[i].rgb, decal * decal * _editor_decal_color[i].a);
}
if (_editor_decal_visible[0] && _editor_decal_size[0] <= _editor_cross_hair_threshold) {
// Crosshair
if (_editor_decal_visible[0] && _editor_decal_size[0] <= _editor_crosshair_threshold) {
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 + 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;
albedo = (h || v) ? mix(albedo, _editor_decal_color[0].rgb, _editor_decal_color[0].a) : albedo;
}
return albedo;
}
Expand Down

0 comments on commit 1425924

Please sign in to comment.