From 026768dff4053e8da15b940c96d2ca048dfb86fd Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Tue, 5 Jan 2021 01:38:38 +1030 Subject: [PATCH] Receptors shader refactor --- scripts/Receptors.gd | 25 +++++++++------ shaders/receptors.shader | 69 ++++++++++++++++++++-------------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/scripts/Receptors.gd b/scripts/Receptors.gd index 443db9e..254f040 100644 --- a/scripts/Receptors.gd +++ b/scripts/Receptors.gd @@ -1,10 +1,12 @@ tool extends MeshInstance2D -var ring_px := 4 # Analogous to diameter -var receptor_px := 24 # Diameter -var shadow_px := 8 # Outer edge, analogous to radius -var shadow_color := Color.black +export var ring_px := 4 # Analogous to diameter +export var receptor_px := 24 # Diameter +export var shadow_px := 8 # Outer edge, analogous to radius +export var line_color := Color.blue +export var dot_color := Color.blue +export var shadow_color := Color.black var center := Vector2(0.0, 0.0) var ring_vertex_count := 36 @@ -108,11 +110,11 @@ func update_ring_mesh(): func _draw(): # draw_old(true, true) -# draw_tris() - var mesh_v = ring_vertex_count - var ring_thickness = receptor_px + shadow_px*2 - var estimated_area = circumscribe_polygon_area(GameTheme.receptor_ring_radius+ring_thickness*0.5, mesh_v) - inscribe_polygon_area(GameTheme.receptor_ring_radius-ring_thickness*0.5, mesh_v) - var ideal_ring_area = PI * (pow(GameTheme.receptor_ring_radius+receptor_px/2+shadow_px, 2) - pow(GameTheme.receptor_ring_radius-receptor_px/2-shadow_px, 2)) + draw_tris() +# var mesh_v = ring_vertex_count +# var ring_thickness = receptor_px + shadow_px*2 +# var estimated_area = circumscribe_polygon_area(GameTheme.receptor_ring_radius+ring_thickness*0.5, mesh_v) - inscribe_polygon_area(GameTheme.receptor_ring_radius-ring_thickness*0.5, mesh_v) +# var ideal_ring_area = PI * (pow(GameTheme.receptor_ring_radius+receptor_px/2+shadow_px, 2) - pow(GameTheme.receptor_ring_radius-receptor_px/2-shadow_px, 2)) var quad_area = 4*pow(GameTheme.receptor_ring_radius+receptor_px/2+shadow_px, 2) @@ -121,6 +123,10 @@ func _draw(): material.set_shader_param("shadow_thickness", shadow_px/GameTheme.receptor_ring_radius) material.set_shader_param("shadow_thickness_taper", -0.75) material.set_shader_param("px", 0.5/GameTheme.receptor_ring_radius) + material.set_shader_param("px2", 1.0/GameTheme.receptor_ring_radius) + material.set_shader_param("line_color", line_color) + material.set_shader_param("dot_color", dot_color) + material.set_shader_param("shadow_color", shadow_color) func set_ring_vertex_count(num: int): assert(num > 3) @@ -143,6 +149,7 @@ func _ready(): $"/root".connect("size_changed", self, "update") #func _process(delta): +# update() # pass # if not Engine.editor_hint: # set_receptor_positions(sin(OS.get_ticks_msec()*0.001*0.0125*PI)*PI) diff --git a/shaders/receptors.shader b/shaders/receptors.shader index edc86d8..40a0e71 100644 --- a/shaders/receptors.shader +++ b/shaders/receptors.shader @@ -15,6 +15,7 @@ uniform float dot_radius = 0.033; uniform float shadow_thickness = 0.01; uniform float shadow_thickness_taper = 0.33; uniform float px = 0.002; // Represents 1px in UV space, for AA purposes +uniform float px2 = 0.004; // Represents 2px in UV space, for AA purposes //void vertex() { //} @@ -25,45 +26,45 @@ float angle_diff(float a, float b) { return d; } -void fragment() { - if (COLOR.rgba != vec4(1.0, 0.0, 0.0, 1.0)) { - COLOR.rgba = vec4(0.0); - lowp float dist = distance(UV, vec2(0.0)); - lowp float angle = atan(-UV.y, UV.x); - float line_alpha = 0.0; - float dot_alpha = 0.0; - float shadow_alpha = 0.0; - float px2 = px/2.0; - - float diff = abs(dist - 1.0); - float d2 = diff - line_thickness; - if (d2 < -px2){ - line_alpha = 1.0; - } else if (d2 < shadow_thickness){ - if (d2 < px2) - line_alpha = 1.0 - (d2 + px2)/px; - shadow_alpha = 1.0 - min((d2 - shadow_thickness*shadow_thickness_taper)/(shadow_thickness*(1.0-shadow_thickness_taper)), 1.0); - } - +vec2 line_alpha(float dist) { + // Returns [line, shadow] + vec2 output = vec2(0.0); + float d = abs(dist - 1.0) - line_thickness; + output.x = clamp(-d/px - 1.0, 0.0, 1.0); + output.y = clamp(1.0 - (d - shadow_thickness*shadow_thickness_taper)/(shadow_thickness*(1.0-shadow_thickness_taper)), 0, 1.0); + return output; +} + +vec2 dot_alpha(vec2 uv) { + // Returns [dot, shadow] + vec2 output = vec2(0.0); // Iterate over all the receptors and check distance to them float receptor_spacing = TAU/float(num_receptors); for (float rads=receptor_offset; rads