Last active
December 2, 2025 18:08
-
-
Save Kielan/5a9f3c10759b71b85ce09351620d3672 to your computer and use it in GitHub Desktop.
Subdivide rust bevy as per ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use bevy::prelude::*; | |
| use std::collections::HashMap; | |
| // | |
| // --------------- Planet Data Resource --------------- | |
| // | |
| #[derive(Resource, Clone)] | |
| pub struct PlanetData { | |
| pub radius: f32, | |
| pub lod_focus: Vec3, | |
| pub max_lod: usize, | |
| /// lod_levels[i].distance, lod_levels[i].resolution | |
| pub lod_levels: Vec<LodLevel>, | |
| pub min_height: f32, | |
| pub max_height: f32, | |
| pub planet_color: Color, | |
| } | |
| #[derive(Clone)] | |
| pub struct LodLevel { | |
| pub distance: f32, | |
| pub resolution: usize, | |
| } | |
| impl PlanetData { | |
| /// Equivalent to Godot's planet_data.point_on_planet(dir) | |
| pub fn point_on_planet(&self, dir: Vec3) -> Vec3 { | |
| dir.normalize() * self.radius | |
| } | |
| } | |
| // | |
| // --------------- Quadtree Chunk Struct --------------- | |
| // | |
| #[derive(Clone)] | |
| pub struct QuadtreeChunk { | |
| pub bounds_pos: Vec3, | |
| pub bounds_size: Vec3, | |
| pub depth: usize, | |
| pub max_depth: usize, | |
| pub identifier: String, | |
| pub children: Vec<QuadtreeChunk>, | |
| } | |
| impl QuadtreeChunk { | |
| pub fn new(bounds_pos: Vec3, bounds_size: Vec3, depth: usize, max_depth: usize) -> Self { | |
| let identifier = format!( | |
| "{}_{}_{}", | |
| bounds_pos, bounds_size, depth | |
| ); | |
| Self { | |
| bounds_pos, | |
| bounds_size, | |
| depth, | |
| max_depth, | |
| identifier, | |
| children: Vec::new(), | |
| } | |
| } | |
| pub fn subdivide( | |
| &mut self, | |
| focus_point: Vec3, | |
| face_origin: Vec3, | |
| axis_a: Vec3, | |
| axis_b: Vec3, | |
| planet: &PlanetData, | |
| ) { | |
| let half_size = self.bounds_size.x * 0.5; | |
| let quarter_size = self.bounds_size.x * 0.25; | |
| let half_extents = Vec3::splat(half_size); | |
| let offsets = [ | |
| Vec2::new(-quarter_size, -quarter_size), | |
| Vec2::new(quarter_size, -quarter_size), | |
| Vec2::new(-quarter_size, quarter_size), | |
| Vec2::new(quarter_size, quarter_size), | |
| ]; | |
| for offset in offsets { | |
| let child_pos_2d = | |
| Vec2::new(self.bounds_pos.x, self.bounds_pos.z) + offset; | |
| let local_center = | |
| face_origin + axis_a * child_pos_2d.x + axis_b * child_pos_2d.y; | |
| let sphere_pos = planet.point_on_planet(local_center.normalize()); | |
| let distance = sphere_pos.distance(focus_point); | |
| let should_subdivide_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment