fixes to uvs
wip on custom chunk shader
This commit is contained in:
@@ -29,7 +29,7 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed:
|
|||||||
&cfg,
|
&cfg,
|
||||||
&noise,
|
&noise,
|
||||||
);
|
);
|
||||||
result[x + z * Chunk::SIZE] = sample.floor();
|
result[x + z * Chunk::SIZE] = sample;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Chunk {
|
return Chunk {
|
||||||
|
|||||||
@@ -71,11 +71,10 @@ fn create_tile(
|
|||||||
let tex_x = texture_index % 11;
|
let tex_x = texture_index % 11;
|
||||||
let tex_y = texture_index / 11;
|
let tex_y = texture_index / 11;
|
||||||
let x_min = tex_x as f32 / 11.;
|
let x_min = tex_x as f32 / 11.;
|
||||||
let x_max = (tex_x + 1) as f32 / 11.;
|
|
||||||
let y_min = tex_y as f32 / 8.;
|
let y_min = tex_y as f32 / 8.;
|
||||||
let tx_unit = x_max - x_min;
|
const TX_UNIT: Vec2 = Vec2::new(1. / 11., 1. / 8.);
|
||||||
|
|
||||||
let uv_offset = Vec2::new(x_min + tx_unit / 2., y_min + tx_unit / 2.);
|
let uv_offset = Vec2::new(x_min + TX_UNIT.x / 2., y_min + TX_UNIT.y / 2.);
|
||||||
|
|
||||||
let idx = verts.len() as u32;
|
let idx = verts.len() as u32;
|
||||||
uvs.push(uv_offset);
|
uvs.push(uv_offset);
|
||||||
@@ -83,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() * TX_UNIT / 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);
|
||||||
@@ -103,7 +102,7 @@ fn create_tile(
|
|||||||
uvs,
|
uvs,
|
||||||
indices,
|
indices,
|
||||||
Vec2::new(x_min, y_min),
|
Vec2::new(x_min, y_min),
|
||||||
Vec2::new(x_min + tx_unit, y_min + tx_unit),
|
Vec2::new(x_min + TX_UNIT.x, y_min + TX_UNIT.y),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
56
game/main/assets/shaders/world/chunk.wgsl
Normal file
56
game/main/assets/shaders/world/chunk.wgsl
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#import bevy_pbr::{
|
||||||
|
forward_io::VertexOutput,
|
||||||
|
mesh_view_bindings::view,
|
||||||
|
pbr_types::{STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT, PbrInput, pbr_input_new},
|
||||||
|
pbr_functions as fns,
|
||||||
|
}
|
||||||
|
#import bevy_core_pipeline::tonemapping::tone_mapping
|
||||||
|
|
||||||
|
@group(2) @binding(0) var my_array_texture: texture_2d_array<f32>;
|
||||||
|
@group(2) @binding(1) var my_array_texture_sampler: sampler;
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fragment(
|
||||||
|
@builtin(front_facing) is_front: bool,
|
||||||
|
mesh: VertexOutput,
|
||||||
|
) -> @location(0) vec4<f32> {
|
||||||
|
let layer = i32(mesh.world_position.x) & 0x3;
|
||||||
|
|
||||||
|
// Prepare a 'processed' StandardMaterial by sampling all textures to resolve
|
||||||
|
// the material members
|
||||||
|
var pbr_input: PbrInput = pbr_input_new();
|
||||||
|
|
||||||
|
pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv, layer);
|
||||||
|
#ifdef VERTEX_COLORS
|
||||||
|
pbr_input.material.base_color = pbr_input.material.base_color * mesh.color;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
|
||||||
|
|
||||||
|
pbr_input.frag_coord = mesh.position;
|
||||||
|
pbr_input.world_position = mesh.world_position;
|
||||||
|
pbr_input.world_normal = fns::prepare_world_normal(
|
||||||
|
mesh.world_normal,
|
||||||
|
double_sided,
|
||||||
|
is_front,
|
||||||
|
);
|
||||||
|
|
||||||
|
pbr_input.is_orthographic = view.projection[3].w == 1.0;
|
||||||
|
|
||||||
|
pbr_input.N = fns::apply_normal_mapping(
|
||||||
|
pbr_input.material.flags,
|
||||||
|
mesh.world_normal,
|
||||||
|
double_sided,
|
||||||
|
is_front,
|
||||||
|
#ifdef VERTEX_TANGENTS
|
||||||
|
#ifdef STANDARD_MATERIAL_NORMAL_MAP
|
||||||
|
mesh.world_tangent,
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
mesh.uv,
|
||||||
|
view.mip_bias,
|
||||||
|
);
|
||||||
|
pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic);
|
||||||
|
|
||||||
|
return tone_mapping(fns::apply_pbr_lighting(pbr_input), view.color_grading);
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use bevy::window::PresentMode;
|
||||||
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
||||||
mod phos;
|
mod phos;
|
||||||
mod prelude;
|
mod prelude;
|
||||||
@@ -13,11 +14,8 @@ fn main() {
|
|||||||
title: "Phos".into(),
|
title: "Phos".into(),
|
||||||
name: Some("phos".into()),
|
name: Some("phos".into()),
|
||||||
resolution: (1920.0, 1080.0).into(),
|
resolution: (1920.0, 1080.0).into(),
|
||||||
resizable: false,
|
resizable: true,
|
||||||
enabled_buttons: bevy::window::EnabledButtons {
|
present_mode: PresentMode::AutoNoVsync,
|
||||||
maximize: false,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
..default()
|
..default()
|
||||||
}),
|
}),
|
||||||
..default()
|
..default()
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use bevy::pbr::MaterialExtension;
|
||||||
|
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;
|
||||||
use iyes_perf_ui::prelude::*;
|
use iyes_perf_ui::prelude::*;
|
||||||
@@ -166,3 +168,16 @@ fn create_map(
|
|||||||
|
|
||||||
commands.insert_resource(heightmap);
|
commands.insert_resource(heightmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||||
|
struct ChunkMaterial {
|
||||||
|
#[texture(0, dimension = "2d_array")]
|
||||||
|
#[sampler(1)]
|
||||||
|
array_texture: Handle<Image>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MaterialExtension for ChunkMaterial {
|
||||||
|
fn fragment_shader() -> ShaderRef {
|
||||||
|
"shaders/world/chunk.wgsl".into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user