6 Commits

Author SHA1 Message Date
cdc3522773 Update mesh_generator.rs 2024-04-27 19:28:35 -04:00
e8cf54a578 merge normals generation 2024-04-27 19:28:25 -04:00
9131f68624 Merge branch 'master' into texture-index 2024-04-27 19:26:45 -04:00
8d109a359a Update assets 2024-04-24 20:08:14 -04:00
a3163684c7 Update assets 2024-04-24 19:38:08 -04:00
b644da1f56 use texture index 2024-04-24 19:37:39 -04:00
6 changed files with 94 additions and 29 deletions

View File

@@ -61,6 +61,7 @@ pub fn generate_chunk_mesh(
let mut uvs = Vec::with_capacity(vertex_count); let mut uvs = Vec::with_capacity(vertex_count);
let mut indices = Vec::with_capacity(vertex_count); let mut indices = Vec::with_capacity(vertex_count);
let mut normals = Vec::with_capacity(vertex_count); let mut normals = Vec::with_capacity(vertex_count);
let mut texture_indicies = Vec::with_capacity(vertex_count);
for z in 0..Chunk::SIZE { for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE { for x in 0..Chunk::SIZE {
@@ -84,6 +85,7 @@ pub fn generate_chunk_mesh(
&mut uvs, &mut uvs,
&mut indices, &mut indices,
&mut normals, &mut normals,
&mut texture_indicies,
// &mut tex, // &mut tex,
tile.texture_id, tile.texture_id,
tile.side_texture_id, tile.side_texture_id,
@@ -97,12 +99,12 @@ pub fn generate_chunk_mesh(
) )
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_attribute(ATTRIBUTE_TEXTURE_INDEX, texture_indicies)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_indices(Indices::U32(indices)); .with_inserted_indices(Indices::U32(indices));
return mesh; return mesh;
} }
const TEX_MULTI: Vec2 = Vec2::new(1000., 1.);
fn create_tile( fn create_tile(
pos: Vec3, pos: Vec3,
neighbors: &[Option<f32>; 6], neighbors: &[Option<f32>; 6],
@@ -110,26 +112,28 @@ fn create_tile(
uvs: &mut Vec<Vec2>, uvs: &mut Vec<Vec2>,
indices: &mut Vec<u32>, indices: &mut Vec<u32>,
normals: &mut Vec<Vec3>, normals: &mut Vec<Vec3>,
texture_indices: &mut Vec<u32>,
texture_index: u32, texture_index: u32,
side_texture_index: u32, side_texture_index: u32,
) { ) {
let uv_offset = Vec2::splat(0.5); let uv_offset = Vec2::splat(0.5);
let tex_off = Vec2::new(texture_index as f32, 0.);
let side_tex_off = Vec2::new(side_texture_index as f32, 0.);
let idx = verts.len() as u32; let idx = verts.len() as u32;
uvs.push((uv_offset / TEX_MULTI) + tex_off); texture_indices.push(texture_index);
uvs.push(uv_offset);
verts.push(pos); verts.push(pos);
normals.push(Vec3::Y); normals.push(Vec3::Y);
for i in 0..6 { for i in 0..6 {
let p = pos + HEX_CORNERS[i]; let p = pos + HEX_CORNERS[i];
verts.push(p); verts.push(p);
let uv = (HEX_CORNERS[i].xz() / 2.) + uv_offset; let uv = (HEX_CORNERS[i].xz() / 2.) + uv_offset;
uvs.push((uv / TEX_MULTI) + tex_off); uvs.push(uv);
indices.push(idx); indices.push(idx);
indices.push(idx + 1 + i as u32); indices.push(idx + 1 + i as u32);
indices.push(idx + 1 + ((i as u32 + 1) % 6)); indices.push(idx + 1 + ((i as u32 + 1) % 6));
normals.push(Vec3::Y); normals.push(Vec3::Y);
texture_indices.push(texture_index);
} }
for i in 0..neighbors.len() { for i in 0..neighbors.len() {
@@ -137,7 +141,17 @@ fn create_tile(
match cur_n { match cur_n {
Some(n_height) => { Some(n_height) => {
if n_height < pos.y { if n_height < pos.y {
create_tile_wall(pos, i, n_height, verts, uvs, indices, normals, side_tex_off); create_tile_wall(
pos,
i,
n_height,
verts,
uvs,
indices,
normals,
texture_indices,
side_texture_index,
);
} }
} }
_ => {} _ => {}
@@ -153,7 +167,8 @@ fn create_tile_wall(
uvs: &mut Vec<Vec2>, uvs: &mut Vec<Vec2>,
indices: &mut Vec<u32>, indices: &mut Vec<u32>,
normals: &mut Vec<Vec3>, normals: &mut Vec<Vec3>,
tex_off: Vec2, texture_indices: &mut Vec<u32>,
texture_index: u32,
) { ) {
let p1 = HEX_CORNERS[(dir) % 6] + pos; let p1 = HEX_CORNERS[(dir) % 6] + pos;
let p2 = HEX_CORNERS[(dir + 1) % 6] + pos; let p2 = HEX_CORNERS[(dir + 1) % 6] + pos;
@@ -167,6 +182,11 @@ fn create_tile_wall(
verts.push(p3); verts.push(p3);
verts.push(p4); verts.push(p4);
texture_indices.push(texture_index);
texture_indices.push(texture_index);
texture_indices.push(texture_index);
texture_indices.push(texture_index);
let n = HEX_NORMALS[dir].normalize(); let n = HEX_NORMALS[dir].normalize();
normals.push(n); normals.push(n);
normals.push(n); normals.push(n);
@@ -181,8 +201,8 @@ fn create_tile_wall(
indices.push(idx + 2); indices.push(idx + 2);
indices.push(idx + 3); indices.push(idx + 3);
uvs.push(Vec2::ZERO + tex_off); uvs.push(Vec2::ZERO);
uvs.push((Vec2::new(1., 0.) / TEX_MULTI) + tex_off); uvs.push(Vec2::new(1., 0.));
uvs.push((Vec2::new(0., pos.y - height) / TEX_MULTI) + tex_off); uvs.push(Vec2::new(0., pos.y - height));
uvs.push((Vec2::new(1., pos.y - height) / TEX_MULTI) + tex_off); uvs.push(Vec2::new(1., pos.y - height));
} }

View File

@@ -11,24 +11,26 @@ use phos::PhosGamePlugin;
fn main() { fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
DefaultPlugins.set(WindowPlugin { DefaultPlugins
primary_window: Some(Window { .set(WindowPlugin {
title: "Phos".into(), primary_window: Some(Window {
name: Some("phos".into()), title: "Phos".into(),
resolution: (1920.0, 1080.0).into(), name: Some("phos".into()),
resizable: true, resolution: (1920.0, 1080.0).into(),
present_mode: PresentMode::AutoNoVsync, resizable: true,
present_mode: PresentMode::AutoNoVsync,
..default()
}),
..default() ..default()
})
.set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Nearest,
..default()
},
}), }),
..default()
}).set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Nearest,
..default()
}
}),
WorldInspectorPlugin::new(), WorldInspectorPlugin::new(),
PhosGamePlugin, PhosGamePlugin,
)) ))

View File

@@ -1,5 +1,4 @@
use bevy::asset::Handle; use bevy::asset::Handle;
use bevy::prelude::{Component, Image, Resource}; use bevy::prelude::{Component, Image, Resource};
#[derive(Resource)] #[derive(Resource)]

View File

@@ -0,0 +1,43 @@
use bevy::{
asset::{Asset, Handle},
pbr::{MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline},
prelude::*,
reflect::TypePath,
render::{
mesh::MeshVertexBufferLayout,
render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
},
texture::Image,
},
};
use world_generation::prelude::ATTRIBUTE_TEXTURE_INDEX;
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct ChunkMaterial {
#[texture(100, dimension = "2d_array")]
#[sampler(101)]
pub array_texture: Handle<Image>,
}
impl MaterialExtension for ChunkMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/world/chunk.wgsl".into()
}
fn specialize(
_pipeline: &MaterialExtensionPipeline,
descriptor: &mut RenderPipelineDescriptor,
layout: &MeshVertexBufferLayout,
_key: MaterialExtensionKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
let vertex_layout = layout.get_layout(&[
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
Mesh::ATTRIBUTE_UV_0.at_shader_location(1),
Mesh::ATTRIBUTE_NORMAL.at_shader_location(2),
ATTRIBUTE_TEXTURE_INDEX.at_shader_location(7),
])?;
descriptor.vertex.buffers = vec![vertex_layout];
Ok(())
}
}

View File

@@ -0,0 +1 @@
pub mod chunk;