Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Godot 4.x #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Voronoi Godot integration
=========================
Voronoi Godot integration for Godot 4.x
=======================================

This fork upgrades the orginal [library](https://github.com/rakai93/godot_voronoi) to support Godot 4.x.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be re-written to exclude "this fork" before being merged back.

It compiles against the 3.5 and master branches of [godot](https://github.com/godotengine/godot).

Integration of [JCash's Voronoi implementation](https://github.com/JCash/voronoi) as a Godot V3.1.1 module.

Expand All @@ -15,22 +18,38 @@ Example usage
-----------------

```gdscript
# Create voronoi generator
var generator = Voronoi.new()
generator.set_points(list_of_vector2)
# optional: set boundaries for diagram, otherwise boundaries are computed based on points
generator.set_boundaries(rect2_bounds)
# optional: relax points N times, resulting in more equal sites
generator.relax_points(2)

# Generate diagram
var diagram = generator.generate_diagram()

# Iterate over sites
for site in diagram.sites():
draw_circle(site.center())

# Iterate over edges
for edge in diagram.edges():
draw_line(edge.start(), edge.end())
#VoronoiTest.gd
extends Node2D

var diagram

func _ready():
# Create voronoi generator
var points = PackedVector2Array([Vector2(0,0)])
for i in 100:
randomize()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

randomize() only needs to be called once before the loop

points.append(Vector2(randi_range(0, 1000), randi_range(0, 1000)))

var generator = Voronoi.new()
generator.set_points(points)
# optional: set boundaries for diagram, otherwise boundaries are computed based on points
#generator.set_boundaries(rect2_bounds)
# optional: relax points N times, resulting in more equal sites
generator.relax_points(2)

# Generate diagram
diagram = generator.generate_diagram()

func _draw():
# Iterate over sites
for site in diagram.sites():
randomize()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

var center = site.center()
#print("site: ", site, "center: ", center)
draw_circle(center, 5, Color(randf(), randf(), randf()))

# Iterate over edges
for edge in diagram.edges():
draw_line(edge.start(), edge.end(), Color.BLACK, 2.0)

```
22 changes: 12 additions & 10 deletions register_types.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "register_types.h"
#include "voronoi.h"

void register_voronoi_types() {

ClassDB::register_class<Voronoi>();
ClassDB::register_class<VoronoiDiagram>();
ClassDB::register_class<VoronoiSite>();
ClassDB::register_class<VoronoiEdge>();

void initialize_voronoi_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<Voronoi>();
ClassDB::register_class<VoronoiDiagram>();
ClassDB::register_class<VoronoiSite>();
ClassDB::register_class<VoronoiEdge>();
}

void unregister_voronoi_types() {

void uninitialize_voronoi_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}

6 changes: 4 additions & 2 deletions register_types.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
void register_voronoi_types();
void unregister_voronoi_types();
#include "modules/register_module_types.h"

void initialize_voronoi_module(ModuleInitializationLevel p_level);
void uninitialize_voronoi_module(ModuleInitializationLevel p_level);
16 changes: 8 additions & 8 deletions voronoi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include <core/math/vector2.h>
#include <core/math/rect2.h>
#include <core/object.h>
#include <core/reference.h>
#include <core/variant.h>
#include <core/vector.h>
#include <core/object/object.h>
#include <core/object/ref_counted.h>
#include <core/variant/variant.h>
#include <core/templates/vector.h>

#include "lib/src/jc_voronoi.h"

Expand Down Expand Up @@ -102,8 +102,8 @@ class VoronoiSite : public Object {
static void _bind_methods();
};

class VoronoiDiagram : public Reference {
GDCLASS(VoronoiDiagram, Reference)
class VoronoiDiagram : public RefCounted {
GDCLASS(VoronoiDiagram, RefCounted)

public:
jcv_diagram _diagram;
Expand All @@ -126,8 +126,8 @@ class VoronoiDiagram : public Reference {
static void _bind_methods();
};

class Voronoi : public Reference {
GDCLASS(Voronoi, Reference)
class Voronoi : public RefCounted {
GDCLASS(Voronoi, RefCounted)

jcv_rect _boundaries;
bool _has_boundaries;
Expand Down