assign random color per triangle

This commit is contained in:
Joe Ardent 2023-03-31 15:28:48 -07:00
parent a5d31d68d6
commit 5a57779261

View file

@ -1,15 +1,20 @@
//use core::slice::SlicePattern;
use bevy::{
prelude::{shape::Icosphere, *},
render::mesh::Indices,
render::{color::Color, mesh::Indices},
};
use bevy_rapier3d::prelude::*;
use hexasphere::shapes::IcoSphere;
use noise::{HybridMulti, NoiseFn, SuperSimplex};
use rand::{Rng, SeedableRng};
use wgpu::PrimitiveTopology;
use crate::Label;
pub const PLANET_RADIUS: f32 = 3_000.0;
pub const PLANET_HUE: f32 = 31.0;
pub const PLANET_SATURATION: f32 = 1.0;
#[derive(Component)]
pub struct CyberPlanet;
@ -19,7 +24,7 @@ fn spawn_planet(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let color = Color::rgb(0.74, 0.5334, 0.176);
//let color = Color::rgb(0.74, 0.5334, 0.176);
let isphere = Icosphere {
radius: PLANET_RADIUS,
subdivisions: 88,
@ -41,14 +46,7 @@ fn spawn_planet(
commands
.spawn(PbrBundle {
mesh: meshes.add(mesh),
material: materials.add(StandardMaterial {
base_color: color,
metallic: 0.3,
perceptual_roughness: 0.3,
//alpha_mode: AlphaMode::Opaque,
..Default::default()
}),
material: materials.add(Color::WHITE.into()),
..Default::default()
})
.insert(pbody)
@ -82,11 +80,13 @@ fn gen_planet(sphere: Icosphere) -> (Mesh, Collider) {
let noise = HybridMulti::<SuperSimplex>::default();
let (mut min, mut max) = (f32::MAX, f32::MIN);
let noisy_points = generated
.raw_points()
.iter()
.map(|&p| {
let disp = noise.get(p.as_dvec3().into()) as f32 * 0.01;
let disp = noise.get(p.as_dvec3().into()) as f32 * 0.05;
let pt = p + (p.normalize() * disp);
pt.into()
})
@ -97,6 +97,13 @@ fn gen_planet(sphere: Icosphere) -> (Mesh, Collider) {
.map(|&p| (Vec3::from_slice(&p) * sphere.radius).into())
.collect::<Vec<[f32; 3]>>();
for p in &points {
let v = Vec3::new(p[0], p[1], p[2]);
let v = v.length();
min = v.min(min);
max = v.max(max);
}
let uvs = generated.raw_data().to_owned();
let mut indices = Vec::with_capacity(generated.indices_per_main_triangle() * 20);
@ -110,13 +117,53 @@ fn gen_planet(sphere: Icosphere) -> (Mesh, Collider) {
idxs.push([idx[0], idx[1], idx[2]]);
}
let indices = Indices::U32(indices);
let shape = Collider::trimesh(points.iter().map(|p| Vect::from_slice(p)).collect(), idxs);
let indices = Indices::U32(indices);
dbg!(&points.len());
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(indices));
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, points);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
//mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
mesh.duplicate_vertices();
mesh.compute_flat_normals();
let tri_list = mesh
.attribute(Mesh::ATTRIBUTE_POSITION)
.unwrap()
.as_float3()
.unwrap();
dbg!(&tri_list.len());
let mut rng = rand::rngs::StdRng::seed_from_u64(57);
let mut colors = Vec::new();
for triangle in tri_list.chunks_exact(3) {
let mut lens = 0.0;
for &v in triangle {
let v = Vec3::new(v[0], v[1], v[2]);
lens += v.length();
}
let v = lens / 3.0;
//let l = val_norm(v, min - 500.0, max); //.powi(2);
let l = 0.41;
let jitter = rng.gen_range(-0.0..=360.0f32);
let h = jitter;
let color = Color::hsl(h, PLANET_SATURATION, l).as_linear_rgba_f32();
for _ in 0..3 {
colors.push(color.clone());
}
}
dbg!(&colors.len());
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
(mesh, shape)
}
/// remaps v in low..high to 0..1
fn val_norm(v: f32, low: f32, high: f32) -> f32 {
// q = (p-A)*(D-C)/(B-A) + C, C = 0, D = 1
(v - low) / (high - low)
}