theoretical basic cube

This commit is contained in:
Lillian Vixe 2024-02-15 05:42:51 -08:00
parent d663c3619a
commit 5f84725afc
2 changed files with 190 additions and 52 deletions

View file

@ -1,5 +0,0 @@
{
"rust-analyzer.linkedProjects": [
".\\Cargo.toml"
]
}

View file

@ -2,30 +2,41 @@ pub mod mesh_plugin {
use bevy::{
prelude::*,
render::{
mesh::{
Indices, VertexAttributeValues
},
mesh::{Indices, VertexAttributeValues},
render_resource::PrimitiveTopology,
},
};
#[derive(Event)]
pub struct SendMeshEvent {
}
pub struct SendMeshEvent {}
pub struct MeshPlugin;
impl Plugin for MeshPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Startup, setup
);
app.add_systems(Startup, setup);
}
}
fn as_mesh(
vertices: Vec<[f32; 3]>,
indices: Vec<u32>,
normals: Vec<[f32; 3]>,
colors: Vec<[f32; 4]>,
) -> Mesh {
Mesh::new(PrimitiveTopology::TriangleList)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
.with_inserted_attribute(Mesh::ATTRIBUTE_COLOR, colors)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_indices(Some(Indices::U32(indices)))
}
fn emit_cube_at(
pos: Vec3,
mut subject: Vec<[f64; 3]>,
vertices: &mut Vec<[f32; 3]>,
indices: &mut Vec<u32>,
normals: &mut Vec<[f32; 3]>,
colors: &mut Vec<[f32; 4]>,
color: Color,
render_px: bool,
render_nx: bool,
render_py: bool,
@ -33,47 +44,143 @@ pub mod mesh_plugin {
render_pz: bool,
render_nz: bool,
) {
if render_px {
subject.push([0.5, -0.5, -0.5]);
subject.push([0.5, -0.5, 0.5]);
subject.push([0.5, 0.5, 0.5]);
subject.push([0.5, 0.5, -0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x + 0.5, pos.y - 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y - 0.5, pos.z + 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z + 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z - 0.5],
]);
normals.extend([
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
}
if render_nx {
subject.push([-0.5, -0.5, -0.5]);
subject.push([-0.5, -0.5, 0.5]);
subject.push([-0.5, 0.5, 0.5]);
subject.push([-0.5, 0.5, -0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x - 0.5, pos.y - 0.5, pos.z - 0.5],
[pos.x - 0.5, pos.y - 0.5, pos.z + 0.5],
[pos.x - 0.5, pos.y + 0.5, pos.z + 0.5],
[pos.x - 0.5, pos.y + 0.5, pos.z - 0.5],
]);
normals.extend([
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
}
if render_py {
subject.push([-0.5, 0.5, -0.5]);
subject.push([0.5, 0.5, -0.5]);
subject.push([0.5, 0.5, 0.5]);
subject.push([-0.5, 0.5, 0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x - 0.5, pos.y + 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z + 0.5],
[pos.x - 0.5, pos.y + 0.5, pos.z + 0.5],
]);
normals.extend([
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
// ^ this is the only one that we know for sure is correct
}
if render_ny {
subject.push([-0.5, -0.5, -0.5]);
subject.push([0.5, -0.5, -0.5]);
subject.push([0.5, -0.5, 0.5]);
subject.push([-0.5, -0.5, 0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x - 0.5, pos.y - 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y - 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y - 0.5, pos.z + 0.5],
[pos.x - 0.5, pos.y - 0.5, pos.z + 0.5],
]);
normals.extend([
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
}
if render_pz {
subject.push([-0.5, -0.5, 0.5]);
subject.push([-0.5, 0.5, 0.5]);
subject.push([0.5, 0.5, 0.5]);
subject.push([0.5, -0.5, 0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x - 0.5, pos.y - 0.5, pos.z + 0.5],
[pos.x - 0.5, pos.y + 0.5, pos.z + 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z + 0.5],
[pos.x + 0.5, pos.y - 0.5, pos.z + 0.5],
]);
normals.extend([
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
}
if render_nz {
subject.push([-0.5, -0.5, -0.5]);
subject.push([-0.5, 0.5, -0.5]);
subject.push([0.5, 0.5, -0.5]);
subject.push([0.5, -0.5, -0.5]);
let len = vertices.len() as u32;
vertices.extend([
[pos.x - 0.5, pos.y - 0.5, pos.z - 0.5],
[pos.x - 0.5, pos.y + 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y + 0.5, pos.z - 0.5],
[pos.x + 0.5, pos.y - 0.5, pos.z - 0.5],
]);
normals.extend([
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
]);
colors.extend([
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
}
}
@ -81,18 +188,12 @@ pub mod mesh_plugin {
mut events: EventReader<SendMeshEvent>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut asset_server: ResMut<AssetServer>
mut asset_server: ResMut<AssetServer>,
) {
/*
in the send mesh event will be the handle of what we update if it is already present, and the mesh data in an octtree or other voxel format
*/
let mut verts = Vec::new();
emit_cube_at(Vec3::ZERO, verts, true, true, true, true, true, true);
}
fn setup(
mut commands: Commands,
@ -100,6 +201,48 @@ pub mod mesh_plugin {
mut material: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
let mut vertices = Vec::new();
let mut normals = Vec::new();
let mut indices = Vec::new();
let mut colors: Vec<[f32; 4]> = Vec::new();
emit_cube_at(
Vec3::ZERO,
&mut vertices,
&mut indices,
&mut normals,
&mut colors,
Color::RED,
true,
true,
true,
true,
true,
true,
);
let test_mesh_handle: Handle<Mesh> =
meshes.add(as_mesh(vertices, indices, normals, colors));
commands.spawn(PbrBundle {
mesh: test_mesh_handle,
..default()
});
let camera_and_light_transform =
Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y);
commands.spawn(Camera3dBundle {
transform: camera_and_light_transform,
..default()
});
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1000.0,
range: 100.0,
..default()
},
transform: camera_and_light_transform,
..default()
});
}
}