shader testing

This commit is contained in:
2024-04-06 00:14:00 -04:00
parent 0bcf65b0b1
commit a8f385630d
3 changed files with 28 additions and 20 deletions

View File

@@ -74,7 +74,7 @@ fn create_tile(
let y_min = tex_y as f32 / 8.; let y_min = tex_y as f32 / 8.;
const TX_UNIT: Vec2 = Vec2::new(1. / 11., 1. / 8.); const TX_UNIT: Vec2 = Vec2::new(1. / 11., 1. / 8.);
let uv_offset = Vec2::new(x_min + TX_UNIT.x / 2., y_min + TX_UNIT.y / 2.); let uv_offset = Vec2::splat(0.5);
let idx = verts.len() as u32; let idx = verts.len() as u32;
uvs.push(uv_offset); uvs.push(uv_offset);
@@ -82,7 +82,7 @@ fn create_tile(
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() * TX_UNIT / 2.) + uv_offset; let uv = (HEX_CORNERS[i].xz() / 2.) + uv_offset;
uvs.push(uv); uvs.push(uv);
indices.push(idx); indices.push(idx);
indices.push(idx + 1 + i as u32); indices.push(idx + 1 + i as u32);
@@ -94,16 +94,7 @@ 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( create_tile_wall(pos, i, n_height, verts, uvs, indices, Vec2::ZERO, Vec2::ONE);
pos,
i,
n_height,
verts,
uvs,
indices,
Vec2::new(x_min, y_min),
Vec2::new(x_min + TX_UNIT.x, y_min + TX_UNIT.y),
);
} }
} }
_ => {} _ => {}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -1,4 +1,4 @@
use bevy::pbr::MaterialExtension; use bevy::pbr::{ExtendedMaterial, MaterialExtension};
use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::{pbr::CascadeShadowConfig, prelude::*}; use bevy::{pbr::CascadeShadowConfig, prelude::*};
use camera_system::PhosCameraPlugin; use camera_system::PhosCameraPlugin;
@@ -47,11 +47,22 @@ fn init_game(mut commands: Commands) {
}); });
} }
fn load_textures(mut commands: Commands, asset_server: Res<AssetServer>) { fn load_textures(
let main_tex = asset_server.load("textures/world/test2.png"); mut commands: Commands,
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
) {
let main_tex = asset_server.load("textures/world/stack.png");
commands.insert_resource(ChunkAtlas { commands.insert_resource(ChunkAtlas {
handle: main_tex.clone(), handle: main_tex.clone(),
}); });
//todo: wait for texture to load
let image = images.get_mut(&main_tex).unwrap();
// Create a new array texture asset from the loaded texture.
let array_layers = 7;
image.reinterpret_stacked_2d_as_array(array_layers);
} }
fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) { fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
@@ -84,9 +95,10 @@ fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
} }
} }
//todo: run after textures are ready
fn create_map( fn create_map(
mut commands: Commands, mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
atlas: Res<ChunkAtlas>, atlas: Res<ChunkAtlas>,
) { ) {
@@ -150,15 +162,20 @@ fn create_map(
2, 2,
); );
let chunk_material = materials.add(StandardMaterial { let chunk_material = materials.add(ExtendedMaterial {
base_color_texture: Some(atlas.handle.clone()), base: StandardMaterial {
base_color: Color::WHITE,
..default() ..default()
},
extension: ChunkMaterial {
array_texture: atlas.handle.clone(),
},
}); });
for chunk in &heightmap.chunks { for chunk in &heightmap.chunks {
let mesh = generate_chunk_mesh(&chunk, &heightmap); let mesh = generate_chunk_mesh(&chunk, &heightmap);
let pos = offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32, 0.); let pos = offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32, 0.);
commands.spawn(PbrBundle { commands.spawn(MaterialMeshBundle {
mesh: meshes.add(mesh), mesh: meshes.add(mesh),
material: chunk_material.clone(), material: chunk_material.clone(),
transform: Transform::from_translation(pos), transform: Transform::from_translation(pos),