|
|
|
|
Reply From: |
Xrayez |
There was a suggestion by me and Godot contributor to have this implemented internally, but here’s a version in GDScript that does it for now:
extends Node2D
func draw_circle_custom(radius, maxerror = 0.25):
if radius <= 0.0:
return
var maxpoints = 1024 # I think this is renderer limit
var numpoints = ceil(PI / acos(1.0 - maxerror / radius))
numpoints = clamp(numpoints, 3, maxpoints)
var points = PoolVector2Array([])
for i in numpoints:
var phi = i * PI * 2.0 / numpoints
var v = Vector2(sin(phi), cos(phi))
points.push_back(v * radius)
draw_colored_polygon(points, Color(1.0, 1.0, 1.0))
func _draw():
draw_circle_custom(1000)
The result should be pretty smooth already. You can experiment with maxerror
parameter to fine tune the smoothness/performance. This can be even be hacked to draw hexagons by specifying high maxerror
value!
Ah ! Perfect ! Thank you !
I hope this will be implemented in some future version of Godot. And preferrably also with a way to specify its position. Otherwise, how could you use this in combination with other draw calls such as draw_circle(position, radius, color) ???
You can actually offset drawing with:
void draw_set_transform( Vector2 position, float rotation, Vector2 scale )
From the docs:
Sets a custom transform for drawing via components. Anything drawn afterwards will be transformed by this.
func _draw():
draw_circle_custom(100) # at Vector2(0, 0) relative to canvas item
draw_set_transform(Vector2(200, 0), 0.0, Vector2(1.0, 1.0))`
draw_circle_custom(100) # will be drawn with offset 200 pixels right ->
Or you could just draw the circle on a separate canvas item (Node2D
) as child of the root scene, and simply set it’s position/transform with corresponding property.
Think of drawing as of commands that are passed to the rendering engine, these commands are then interpreted and drawn all in order.
Xrayez | 2019-02-12 14:06
Interesting !
Seems like you really can do just about anything with the Godot Engine. =)