From c11845db90f7767ce15904f93fc9e977c82f12de Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 26 Jan 2022 13:33:48 -0800 Subject: [PATCH] Use bevy_polyline to wireframe the big sphere. --- src/glamor.rs | 46 ++++++++++++++++++++++++++++++++++++++++------ src/lights.rs | 4 ++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/glamor.rs b/src/glamor.rs index 38d35ad..8d2f8f9 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -1,20 +1,52 @@ use bevy::{ pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin}, prelude::*, - render::{options::WgpuOptions, render_resource::WgpuFeatures}, + render::{ + mesh::{Indices, VertexAttributeValues}, + options::WgpuOptions, + render_resource::WgpuFeatures, + }, }; +use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use rand::{thread_rng, Rng}; use crate::{geometry::CyberSphere, lights::AnimateCyberLightWireframe}; fn wireframe_planet( mut commands: Commands, - mut wireframe_config: ResMut, - query: Query>, + meshes: Res>, + mut polylines: ResMut>, + mut polymats: ResMut>, + query: Query<&Handle, With>, ) { - let ent = query.single(); - wireframe_config.global = false; - commands.entity(ent).insert(Wireframe); + let handle = query.single(); + let mesh = meshes.get(handle).unwrap(); + let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap(); + + let mut pts = Vec::with_capacity(vertices.len()); + + if let VertexAttributeValues::Float32x3(verts) = vertices { + let indices = mesh.indices().unwrap(); + if let Indices::U32(indices) = indices { + for i in indices.iter() { + let v = verts[*i as usize]; + let v = Vec3::from_slice(&v); + pts.push(v); + } + } + } + + if !pts.is_empty() { + commands.spawn_bundle(PolylineBundle { + polyline: polylines.add(Polyline { vertices: pts }), + material: polymats.add(PolylineMaterial { + width: 6.0, + color: Color::hsla(292.0, 1.0, 0.60, 1.0), + perspective: false, + }), + ..Default::default() + }); + } } fn wireframify_lights( @@ -45,8 +77,10 @@ impl Plugin for CyberGlamorPlugin { features: WgpuFeatures::POLYGON_MODE_LINE, ..Default::default() }) + .insert_resource(WireframeConfig { global: false }) .add_startup_system_to_stage(StartupStage::PostStartup, wireframe_planet) .add_system(wireframify_lights) + .add_plugin(PolylinePlugin) .add_plugin(WireframePlugin); } } diff --git a/src/lights.rs b/src/lights.rs index 216d0a4..3a3d046 100644 --- a/src/lights.rs +++ b/src/lights.rs @@ -22,8 +22,8 @@ fn spawn_moving_lights( mut materials: ResMut>, ) { let rng = &mut thread_rng(); - // spawn 200 orbiting bisexual lights - for _ in 0..255 { + // spawn orbiting bisexual lights + for _ in 0..511 { // mechanics let axis = crate::random_unit_vec(rng); let angle = rng.gen_range(0.0..TAU);