From 942a025cc1dd1600b3359ad35baa1ae7995a896b Mon Sep 17 00:00:00 2001 From: Isabelle Lesko Date: Mon, 29 Mar 2021 14:16:49 -0500 Subject: [PATCH] some work on the point generation --- Main.tscn | 8 ++++-- PointGenerator.gdns | 8 ++++++ marching_cubes_lib.tres | 7 +++++ src/point_gen.rs | 59 ++++++++++++++++++++++++++++++++--------- 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 PointGenerator.gdns create mode 100644 marching_cubes_lib.tres diff --git a/Main.tscn b/Main.tscn index 578beda..a60dec1 100644 --- a/Main.tscn +++ b/Main.tscn @@ -4,6 +4,10 @@ [node name="RootNode" type="Spatial"] -[node name="PointGenerator" type="Node" parent="."] +[node name="PointGenerator" type="Spatial" parent="."] +script = ExtResource( 1 ) -[node name="PointsRoot" type="Spatial" parent="."] +[node name="CameraRoot" type="Spatial" parent="."] + +[node name="Camera" type="Camera" parent="CameraRoot"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 ) diff --git a/PointGenerator.gdns b/PointGenerator.gdns new file mode 100644 index 0000000..877aec9 --- /dev/null +++ b/PointGenerator.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://marching_cubes_lib.tres" type="GDNativeLibrary" id=1] + +[resource] +resource_name = "PointGenerator" +class_name = "PointGenerator" +library = ExtResource( 1 ) diff --git a/marching_cubes_lib.tres b/marching_cubes_lib.tres new file mode 100644 index 0000000..4cba7ad --- /dev/null +++ b/marching_cubes_lib.tres @@ -0,0 +1,7 @@ +[gd_resource type="GDNativeLibrary" format=2] + +[resource] +entry/X11.64 = "res://target/debug/libmarching_cubes.so" +entry/X11.32 = "res://target/debug/libmarching_cubes.so" +dependency/X11.64 = [ ] +dependency/X11.32 = [ ] diff --git a/src/point_gen.rs b/src/point_gen.rs index e47c630..2b062da 100644 --- a/src/point_gen.rs +++ b/src/point_gen.rs @@ -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, f64>, + _point_map: HashMap, 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"); } }