-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_generator.cpp
190 lines (158 loc) · 5.36 KB
/
fn_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "fn_generator.h"
FNGenerator::FNGenerator(int type) {
_gen_type = static_cast<FNGenerator::GeneratorType>(type);
_init_node();
}
Ref<FNGenerator> FNGenerator::new_generator(int type) {
return Ref<FNGenerator>(new FNGenerator(type));
}
PackedFloat32Array FNGenerator::gen_uniform_grid_2D(
int x_start, int y_start,
int width, int height,
float frequency, int seed) const {
PackedFloat32Array output;
output.resize_zeroed(width * height);
_node->GenUniformGrid2D(output.ptrw(),
x_start, y_start,
width, height,
frequency, seed);
return output;
}
Ref<Image> FNGenerator::gen_uniform_2D_image(
int x_start, int y_start,
int width, int height,
float frequency, int seed
) const {
auto src_node = _get_smart_node();
_FastNoise::SmartNode<_FastNoise::ConvertRGBA8> gen_rgba8;
gen_rgba8 = _FastNoise::New<_FastNoise::ConvertRGBA8>(src_node->GetSIMDLevel());
gen_rgba8->SetSource(src_node);
PackedFloat32Array data;
data.resize_zeroed(width * height);
gen_rgba8->GenUniformGrid2D(data.ptrw(),
x_start, y_start,
width, height,
frequency, seed
);
Ref<Image> img = Image::create_from_data(width, height,
false, Image::Format::FORMAT_RGBA8,
data.to_byte_array());
return img;
}
Ref<ArrayMesh> FNGenerator::gen_mesh_2D(int x_start, int y_start, int width, int height, float frequency, int seed) const {
PackedFloat32Array noise;
noise.resize_zeroed(width * height);
_node->GenUniformGrid2D(noise.ptrw(),
x_start, y_start,
width, height,
frequency, seed
);
PackedVector3Array verts;
PackedVector2Array uvs;
PackedVector3Array normals;
PackedInt32Array indices;
Array mesh_array;
mesh_array.resize(Mesh::ARRAY_MAX);
mesh_array[Mesh::ARRAY_VERTEX] = verts;
mesh_array[Mesh::ARRAY_TEX_UV] = uvs;
mesh_array[Mesh::ARRAY_NORMAL] = normals;
mesh_array[Mesh::ARRAY_INDEX] = indices;
Ref<ArrayMesh> mesh;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
return mesh;
}
PackedFloat32Array FNGenerator::gen_uniform_grid_3D(
int x_start, int y_start, int z_start,
int width, int height, int depth,
float frequency, int seed) const {
PackedFloat32Array noise_output;
noise_output.resize_zeroed(width * height * depth);
_node->GenUniformGrid3D(noise_output.ptrw(),
x_start, y_start, z_start,
width, height, depth,
frequency, seed);
return noise_output;
}
int FNGenerator::get_type() const {
return static_cast<int>(_gen_type);
}
void FNGenerator::set_type(int type) {
_gen_type = static_cast<FNGenerator::GeneratorType>(type);
_init_node();
}
void FNGenerator::_bind_methods() {
_bind_generator_type_enum();
ClassDB::bind_static_method("FNGenerator",
D_METHOD("new_generator", "gen_type"),
&FNGenerator::new_generator);
ClassDB::bind_method(D_METHOD("gen_uniform_grid_2D",
"x_start", "y_start", "width", "height",
"frequency", "seed"), &FNGenerator::gen_uniform_grid_2D);
ClassDB::bind_method(D_METHOD("gen_uniform_2D_image",
"x_start", "y_start", "width", "height",
"frequency", "seed"), &FNGenerator::gen_uniform_2D_image);
ClassDB::bind_method(D_METHOD("gen_uniform_grid_3D",
"x_start", "y_start", "z_start",
"width", "height", "depth",
"frequency", "seed"), &FNGenerator::gen_uniform_grid_3D);
ClassDB::bind_method(
D_METHOD("get_type"),
&FNGenerator::get_type
);
ClassDB::bind_method(
D_METHOD("set_type", "type"),
&FNGenerator::set_type
);
}
void FNGenerator::_bind_generator_type_enum() {
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"Simplex",static_cast<int64_t>(GeneratorType::Simplex)
);
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"Perlin", static_cast<int64_t>(GeneratorType::Perlin)
);
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"CellularValue", static_cast<int64_t>(GeneratorType::CellularValue)
);
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"CellularDistance", static_cast<int64_t>(GeneratorType::CellularDistance)
);
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"CellularLookup", static_cast<int64_t>(GeneratorType::CellularLookup)
);
ClassDB::bind_integer_constant(
"FNGenerator", "GeneratorType",
"Value", static_cast<int64_t>(GeneratorType::Value)
);
}
void FNGenerator::_init_node() {
if(_node) {
_node.reset();
}
switch(_gen_type) {
case Simplex:
default:
_node = _FastNoise::New<_FastNoise::Simplex>();
break;
case Perlin:
_node = _FastNoise::New<_FastNoise::Perlin>();
break;
case CellularValue:
_node = _FastNoise::New<_FastNoise::CellularValue>();
break;
case CellularDistance:
_node = _FastNoise::New<_FastNoise::CellularDistance>();
break;
case CellularLookup:
_node = _FastNoise::New<_FastNoise::CellularLookup>();
break;
case Value:
_node = _FastNoise::New<_FastNoise::Value>();
break;
}
}