From 0fd8abf53b7e8d9082fb870b4ec99e4cc59581aa Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 18 Dec 2019 15:59:56 +1030 Subject: [PATCH] draw_polyline works on 3.1.2 --- Menu.gd | 2 +- Receptors.gd | 77 +++++++++++++++++++++++++++++++++------------------- main.tscn | 6 ++-- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/Menu.gd b/Menu.gd index 8230e10..fc7484e 100644 --- a/Menu.gd +++ b/Menu.gd @@ -1,4 +1,4 @@ -tool +#tool extends Node2D var song_defs = {} diff --git a/Receptors.gd b/Receptors.gd index 407ce5a..7eb57c4 100644 --- a/Receptors.gd +++ b/Receptors.gd @@ -1,4 +1,4 @@ -tool +#tool extends Node2D var ring_px := 4 @@ -7,7 +7,7 @@ var shadow_px := 5 var shadow_color := Color.black var center := Vector2(0.0, 0.0) -func make_ring_mesh(inner_vertices: int, thickness: float, radius: float, skew=0.5): +func make_ring_mesh(inner_vertices: int, thickness: float, radius: float, skew:=0.5, repeat_start:=true): # This makes a trianglestrip around the ring, consisting of chords on the inside and tangents on the outside. # The goal is to exchange some fragment and vertex processing load: # - a full quad of the ring would be the maximum fragment load and minimum vertex load @@ -30,16 +30,30 @@ func make_ring_mesh(inner_vertices: int, thickness: float, radius: float, skew=0 var r2 = (radius + thickness*0.5)/sin((PI-angle_increment)/2) var vertex_list = PoolVector2Array() + var inner_list = PoolVector2Array() + var outer_list = PoolVector2Array() for i in inner_vertices: var angle_i = i * angle_increment var angle_o = angle_i + angle_outer_offset vertex_list.push_back(polar2cartesian(r1, angle_i)) vertex_list.push_back(polar2cartesian(r2, angle_o)) - return vertex_list + inner_list.push_back(vertex_list[-2]) + outer_list.push_back(vertex_list[-1]) + if repeat_start: + vertex_list.push_back(vertex_list[0]) + vertex_list.push_back(vertex_list[1]) + inner_list.push_back(vertex_list[0]) + outer_list.push_back(vertex_list[1]) + return [vertex_list, inner_list, outer_list] func triangle_area(a: Vector2, b: Vector2, c: Vector2) -> float: return 0.5 * abs((a.x-c.x)*(b.y-a.y) - (a.x-b.x)*(c.y-a.y)) - + +func inscribe_polygon_area(r: float, sides: int) -> float: + return 0.5 * sides * r * r * sin(TAU/sides) +func circumscribe_polygon_area(r: float, sides: int) -> float: + return sides * r * r * tan(PI/sides) + func arc_point_list(center: Vector2, radius: float, angle_from:=0.0, angle_to:=360.0, points:=90) -> PoolVector2Array: var point_list = PoolVector2Array() for i in range(points): @@ -48,38 +62,45 @@ func arc_point_list(center: Vector2, radius: float, angle_from:=0.0, angle_to:=3 point_list.push_back(center + polar2cartesian(radius, angle)) return point_list -func _draw(): - # Receptor ring +func draw_old(circles:=true, shadows:=true): # Receptor ring var receptor_circle := arc_point_list(center, GameTheme.receptor_ring_radius, 0.0, 360.0, 360) var receptor_centers := arc_point_list(center, GameTheme.receptor_ring_radius, Rules.FIRST_COLUMN_ANGLE_DEG, Rules.FIRST_COLUMN_ANGLE_DEG+360.0-Rules.COLS_ANGLE_DEG, Rules.COLS) -# # Shadows -# for i in range(len(receptor_circle)-1): -# draw_line(receptor_circle[i], receptor_circle[i+1], shadow_color, ring_px + shadow_px, true) -## draw_line(receptor_circle[i], receptor_circle[i+1], shadow_color) -# for i in range(len(receptor_centers)): -# draw_circle(receptor_centers[i], (receptor_px + shadow_px)/2, shadow_color) -# -# # Foregrounds - for i in range(len(receptor_circle)-1): - draw_line(receptor_circle[i], receptor_circle[i+1], GameTheme.receptor_color, ring_px, true) -# draw_line(receptor_circle[i], receptor_circle[i+1], GameTheme.receptor_color) - for i in range(len(receptor_centers)): - draw_circle(receptor_centers[i], receptor_px/2, GameTheme.receptor_color) - + if shadows: + #draw_polyline(receptor_circle, shadow_color, ring_px + shadow_px, true) + draw_polyline(receptor_circle, Color.darkblue, ring_px + shadow_px, true) + if circles: + for i in range(len(receptor_centers)): +# draw_circle(receptor_centers[i], (receptor_px + shadow_px)/2, shadow_color) + draw_circle(receptor_centers[i], (receptor_px + shadow_px)/2, Color.darkblue) + + draw_polyline(receptor_circle, GameTheme.receptor_color, ring_px, true) + if circles: + for i in range(len(receptor_centers)): + draw_circle(receptor_centers[i], receptor_px/2, GameTheme.receptor_color) + + +func _draw(): + draw_old(true, true) var mesh_v = $VerticesSlider.value var skew = $SkewSlider.value var dbg_color = Color.red - var ring_vertices = make_ring_mesh(mesh_v, receptor_px + shadow_px, GameTheme.receptor_ring_radius, skew) - var estimated_area = 0.0 + var ring_thickness = receptor_px + shadow_px + var ring_vertices = make_ring_mesh(mesh_v, ring_thickness, GameTheme.receptor_ring_radius, skew) + 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+shadow_px)/2, 2) - pow(GameTheme.receptor_ring_radius-(receptor_px+shadow_px)/2, 2)) - var l = len(ring_vertices) - for i in l: - draw_line(ring_vertices[i], ring_vertices[(i+1)%l], dbg_color) - draw_line(ring_vertices[i], ring_vertices[(i+2)%l], dbg_color, 2.0) - estimated_area += triangle_area(ring_vertices[i], ring_vertices[(i+1)%l], ring_vertices[(i+2)%l]) + + draw_polyline(ring_vertices[0], dbg_color) + draw_polyline(ring_vertices[1], dbg_color) + draw_polyline(ring_vertices[2], dbg_color) + +# var l = len(ring_vertices) +# for i in l: +## estimated_area += triangle_area(ring_vertices[i], ring_vertices[(i+1)%l], ring_vertices[(i+2)%l]) var quad_area = 4*pow(GameTheme.receptor_ring_radius+(receptor_px+shadow_px)/2, 2) - $"/root/main/InputHandler".text = "Vertices: %d*2 Skew: %.3f\nArea: %.0f\n(%.0f%% ideal ring)\n(%.0f%% quad)"%[mesh_v, skew, estimated_area, 100.0*estimated_area/ideal_ring_area, 100.0*estimated_area/quad_area] + var fps = Performance.get_monitor(Performance.TIME_FPS) + $"/root/main/InputHandler".text = "Vertices: %d*2 Skew: %.3f\nArea: %.0f\n(%.0f%% ideal ring)\n(%.0f%% quad)\nFPS: %.0f"%[mesh_v, skew, estimated_area, 100.0*estimated_area/ideal_ring_area, 100.0*estimated_area/quad_area, fps] +# ._draw() func _ready(): $"/root".connect("size_changed", self, "update") diff --git a/main.tscn b/main.tscn index b74c629..6986f2c 100644 --- a/main.tscn +++ b/main.tscn @@ -62,7 +62,7 @@ __meta__ = { [node name="ScreenFilter" type="Node2D" parent="."] script = ExtResource( 3 ) -[node name="Receptors" type="Node2D" parent="."] +[node name="Receptors" type="MeshInstance2D" parent="."] script = ExtResource( 4 ) [node name="SkewSlider" type="HSlider" parent="Receptors"] @@ -86,9 +86,8 @@ margin_right = 200.0 margin_bottom = -84.0 min_value = 3.0 max_value = 72.0 -value = 8.0 +value = 36.0 tick_count = 2 -ticks_on_borders = false [node name="NoteHandler" type="Node2D" parent="."] script = ExtResource( 5 ) @@ -105,6 +104,7 @@ material = SubResource( 1 ) material = SubResource( 2 ) [node name="Menu" type="Node2D" parent="."] +visible = false script = ExtResource( 9 ) [node name="ScoreText" type="Node2D" parent="Menu"]