|
|
@@ -1,5 +1,6 @@ |
|
|
|
use cgmath::Vector3; |
|
|
|
use gdnative::api::OpenSimplexNoise; |
|
|
|
use cgmath::Vector3 as Vector3Cg; |
|
|
|
use gdnative::api::{MeshInstance, OpenSimplexNoise, SpatialMaterial, SphereMesh}; |
|
|
|
use gdnative::prelude::core_types::Vector3 as Vector3Gd; |
|
|
|
use gdnative::prelude::*; |
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
@@ -24,7 +25,7 @@ pub struct PointGenerator { |
|
|
|
pub noise_persistence: f64, |
|
|
|
|
|
|
|
// internal fields |
|
|
|
_point_map: HashMap<Vector3<i64>, f64>, |
|
|
|
_point_map: HashMap<Vector3Cg<i64>, f64>, |
|
|
|
} |
|
|
|
|
|
|
|
#[methods] |
|
|
@@ -33,12 +34,12 @@ impl PointGenerator { |
|
|
|
fn new(_owner: &Node) -> Self { |
|
|
|
PointGenerator { |
|
|
|
// exported fields |
|
|
|
width: 0, |
|
|
|
height: 0, |
|
|
|
depth: 0, |
|
|
|
width: 3, |
|
|
|
height: 3, |
|
|
|
depth: 3, |
|
|
|
noise_seed: 691337420, |
|
|
|
noise_octaves: 4, |
|
|
|
noise_peroid: 20.0, |
|
|
|
noise_peroid: 1.0, |
|
|
|
noise_persistence: 0.8, |
|
|
|
// internal fields |
|
|
|
_point_map: HashMap::new(), |
|
|
@@ -53,20 +54,52 @@ impl PointGenerator { |
|
|
|
rng.set_period(self.noise_peroid); |
|
|
|
rng.set_persistence(self.noise_persistence); |
|
|
|
|
|
|
|
for x in 0..(self.width) { |
|
|
|
for z in 0..(self.depth) { |
|
|
|
for y in 0..(self.height) { |
|
|
|
let noise_value = rng.get_noise_3d(x as f64, y as f64, z as f64); |
|
|
|
self._point_map.insert(Vector3::new(x, y, z), noise_value); |
|
|
|
for x in (-self.width)..(self.width + 1) { |
|
|
|
for z in (-self.depth)..(self.depth + 1) { |
|
|
|
for y in (-self.height)..(self.height + 1) { |
|
|
|
let noise_value = (rng.get_noise_3d(x as f64, y as f64, z as f64) + 1.0) / 2.0; |
|
|
|
godot_print!("noise: {}", noise_value); |
|
|
|
self._point_map.insert(Vector3Cg::new(x, y, z), noise_value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn generate_test_meshes(&self, owner: &Node) { |
|
|
|
self._point_map.iter().for_each(|(point, noise_value)| { |
|
|
|
let mesh = MeshInstance::new(); |
|
|
|
|
|
|
|
let mesh_data = SphereMesh::new(); |
|
|
|
mesh_data.set_height(0.5); |
|
|
|
mesh_data.set_radius(0.25); |
|
|
|
|
|
|
|
let material = SpatialMaterial::new(); |
|
|
|
material.set_albedo(Color::rgb( |
|
|
|
*noise_value as f32, |
|
|
|
*noise_value as f32, |
|
|
|
*noise_value as f32, |
|
|
|
)); |
|
|
|
|
|
|
|
mesh_data.set_material(material); |
|
|
|
|
|
|
|
let transform = Transform { |
|
|
|
basis: Basis::default(), |
|
|
|
origin: Vector3Gd::new(point.x as f32, point.y as f32, point.z as f32), |
|
|
|
}; |
|
|
|
|
|
|
|
mesh.set_mesh(mesh_data); |
|
|
|
mesh.set_transform(transform); |
|
|
|
|
|
|
|
owner.add_child(mesh, false); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// called on the node being instatiated |
|
|
|
#[export] |
|
|
|
fn _ready(&mut self, _owner: &Node) { |
|
|
|
fn _ready(&mut self, owner: &Node) { |
|
|
|
self.generate_points(); |
|
|
|
godot_print!("generated points"); |
|
|
|
self.generate_test_meshes(owner); |
|
|
|
godot_print!("generated meshes"); |
|
|
|
} |
|
|
|
} |