浏览代码

some work on the point generation

master
Isabelle L. 4 年前
父节点
当前提交
942a025cc1
共有 4 个文件被更改,包括 67 次插入15 次删除
  1. +6
    -2
      Main.tscn
  2. +8
    -0
      PointGenerator.gdns
  3. +7
    -0
      marching_cubes_lib.tres
  4. +46
    -13
      src/point_gen.rs

+ 6
- 2
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 )

+ 8
- 0
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 )

+ 7
- 0
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 = [ ]

+ 46
- 13
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<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");
}
}

正在加载...
取消
保存