assign random color per triangle
This commit is contained in:
parent
a5d31d68d6
commit
5a57779261
1 changed files with 60 additions and 13 deletions
|
@ -1,15 +1,20 @@
|
||||||
|
//use core::slice::SlicePattern;
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
prelude::{shape::Icosphere, *},
|
prelude::{shape::Icosphere, *},
|
||||||
render::mesh::Indices,
|
render::{color::Color, mesh::Indices},
|
||||||
};
|
};
|
||||||
use bevy_rapier3d::prelude::*;
|
use bevy_rapier3d::prelude::*;
|
||||||
use hexasphere::shapes::IcoSphere;
|
use hexasphere::shapes::IcoSphere;
|
||||||
use noise::{HybridMulti, NoiseFn, SuperSimplex};
|
use noise::{HybridMulti, NoiseFn, SuperSimplex};
|
||||||
|
use rand::{Rng, SeedableRng};
|
||||||
use wgpu::PrimitiveTopology;
|
use wgpu::PrimitiveTopology;
|
||||||
|
|
||||||
use crate::Label;
|
use crate::Label;
|
||||||
|
|
||||||
pub const PLANET_RADIUS: f32 = 3_000.0;
|
pub const PLANET_RADIUS: f32 = 3_000.0;
|
||||||
|
pub const PLANET_HUE: f32 = 31.0;
|
||||||
|
pub const PLANET_SATURATION: f32 = 1.0;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CyberPlanet;
|
pub struct CyberPlanet;
|
||||||
|
@ -19,7 +24,7 @@ fn spawn_planet(
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
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 {
|
let isphere = Icosphere {
|
||||||
radius: PLANET_RADIUS,
|
radius: PLANET_RADIUS,
|
||||||
subdivisions: 88,
|
subdivisions: 88,
|
||||||
|
@ -41,14 +46,7 @@ fn spawn_planet(
|
||||||
commands
|
commands
|
||||||
.spawn(PbrBundle {
|
.spawn(PbrBundle {
|
||||||
mesh: meshes.add(mesh),
|
mesh: meshes.add(mesh),
|
||||||
material: materials.add(StandardMaterial {
|
material: materials.add(Color::WHITE.into()),
|
||||||
base_color: color,
|
|
||||||
metallic: 0.3,
|
|
||||||
perceptual_roughness: 0.3,
|
|
||||||
//alpha_mode: AlphaMode::Opaque,
|
|
||||||
..Default::default()
|
|
||||||
}),
|
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.insert(pbody)
|
.insert(pbody)
|
||||||
|
@ -82,11 +80,13 @@ fn gen_planet(sphere: Icosphere) -> (Mesh, Collider) {
|
||||||
|
|
||||||
let noise = HybridMulti::<SuperSimplex>::default();
|
let noise = HybridMulti::<SuperSimplex>::default();
|
||||||
|
|
||||||
|
let (mut min, mut max) = (f32::MAX, f32::MIN);
|
||||||
|
|
||||||
let noisy_points = generated
|
let noisy_points = generated
|
||||||
.raw_points()
|
.raw_points()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&p| {
|
.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);
|
let pt = p + (p.normalize() * disp);
|
||||||
pt.into()
|
pt.into()
|
||||||
})
|
})
|
||||||
|
@ -97,6 +97,13 @@ fn gen_planet(sphere: Icosphere) -> (Mesh, Collider) {
|
||||||
.map(|&p| (Vec3::from_slice(&p) * sphere.radius).into())
|
.map(|&p| (Vec3::from_slice(&p) * sphere.radius).into())
|
||||||
.collect::<Vec<[f32; 3]>>();
|
.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 uvs = generated.raw_data().to_owned();
|
||||||
|
|
||||||
let mut indices = Vec::with_capacity(generated.indices_per_main_triangle() * 20);
|
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]]);
|
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 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);
|
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
|
||||||
mesh.set_indices(Some(indices));
|
mesh.set_indices(Some(indices));
|
||||||
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, points);
|
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)
|
(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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue