array texture shader
This commit is contained in:
@@ -1,56 +1,58 @@
|
|||||||
#import bevy_pbr::{
|
#import bevy_pbr::{
|
||||||
forward_io::VertexOutput,
|
pbr_fragment::pbr_input_from_standard_material,
|
||||||
mesh_view_bindings::view,
|
pbr_functions::alpha_discard,
|
||||||
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>;
|
#ifdef PREPASS_PIPELINE
|
||||||
@group(2) @binding(1) var my_array_texture_sampler: sampler;
|
#import bevy_pbr::{
|
||||||
|
prepass_io::{VertexOutput, FragmentOutput},
|
||||||
|
pbr_deferred_functions::deferred_output,
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#import bevy_pbr::{
|
||||||
|
forward_io::{VertexOutput, FragmentOutput},
|
||||||
|
pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ChunkMaterial {
|
||||||
|
array_texture: texture_2d_array<f32>,
|
||||||
|
array_texture_sampler: sampler,
|
||||||
|
}
|
||||||
|
|
||||||
|
@group(2) @binding(100)
|
||||||
|
var<uniform> chunk_material: ChunkMaterial;
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
fn fragment(
|
fn fragment(
|
||||||
|
in: VertexOutput,
|
||||||
@builtin(front_facing) is_front: bool,
|
@builtin(front_facing) is_front: bool,
|
||||||
mesh: VertexOutput,
|
) -> FragmentOutput {
|
||||||
) -> @location(0) vec4<f32> {
|
// generate a PbrInput struct from the StandardMaterial bindings
|
||||||
let layer = i32(mesh.world_position.x) & 0x3;
|
var pbr_input = pbr_input_from_standard_material(in, is_front);
|
||||||
|
|
||||||
// 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);
|
// alpha discard
|
||||||
#ifdef VERTEX_COLORS
|
pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color);
|
||||||
pbr_input.material.base_color = pbr_input.material.base_color * mesh.color;
|
|
||||||
|
#ifdef PREPASS_PIPELINE
|
||||||
|
// in deferred mode we can't modify anything after that, as lighting is run in a separate fullscreen shader.
|
||||||
|
let out = deferred_output(in, pbr_input);
|
||||||
|
#else
|
||||||
|
var out: FragmentOutput;
|
||||||
|
// apply lighting
|
||||||
|
out.color = apply_pbr_lighting(pbr_input);
|
||||||
|
|
||||||
|
let layer = i32(in.world_position.x) & 0x7;
|
||||||
|
out.color = textureSample(chunk_material.array_texture, chunk_material.array_texture_sampler, in.uv, layer);
|
||||||
|
|
||||||
|
// apply in-shader post processing (fog, alpha-premultiply, and also tonemapping, debanding if the camera is non-hdr)
|
||||||
|
// note this does not include fullscreen postprocessing effects like bloom.
|
||||||
|
out.color = main_pass_post_lighting_processing(pbr_input, out.color);
|
||||||
|
|
||||||
|
// we can optionally modify the final result here
|
||||||
|
out.color = out.color * 2.0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
|
return out;
|
||||||
|
|
||||||
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,3 +1,4 @@
|
|||||||
|
use bevy::asset::LoadState;
|
||||||
use bevy::pbr::{ExtendedMaterial, 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::*};
|
||||||
@@ -14,9 +15,12 @@ pub struct PhosGamePlugin;
|
|||||||
|
|
||||||
impl Plugin for PhosGamePlugin {
|
impl Plugin for PhosGamePlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins(PhosCameraPlugin);
|
app.add_plugins(PhosCameraPlugin).add_plugins(MaterialPlugin::<
|
||||||
|
ExtendedMaterial<StandardMaterial, ChunkMaterial>,
|
||||||
|
>::default());
|
||||||
app.add_systems(Startup, init_game)
|
app.add_systems(Startup, init_game)
|
||||||
.add_systems(Startup, (load_textures, create_map).chain());
|
.add_systems(Startup, (load_textures, create_map).chain());
|
||||||
|
app.add_systems(Update, (check_texture, spawn_map));
|
||||||
app.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
|
app.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
|
||||||
.add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin)
|
.add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin)
|
||||||
.add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin)
|
.add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin)
|
||||||
@@ -45,24 +49,42 @@ fn init_game(mut commands: Commands) {
|
|||||||
transform: Transform::from_xyz(500., 260.0, 500.).looking_at(Vec3::ZERO, Vec3::Y),
|
transform: Transform::from_xyz(500., 260.0, 500.).looking_at(Vec3::ZERO, Vec3::Y),
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
commands.insert_resource(PhosMap::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_textures(
|
fn load_textures(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
asset_server: Res<AssetServer>,
|
asset_server: Res<AssetServer>,
|
||||||
mut images: ResMut<Assets<Image>>,
|
|
||||||
) {
|
) {
|
||||||
let main_tex = asset_server.load("textures/world/stack.png");
|
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(),
|
||||||
|
is_loaded: false,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
//todo: wait for texture to load
|
fn check_texture(
|
||||||
let image = images.get_mut(&main_tex).unwrap();
|
asset_server: Res<AssetServer>,
|
||||||
|
mut atlas: ResMut<ChunkAtlas>,
|
||||||
|
mut map: ResMut<PhosMap>,
|
||||||
|
mut images: ResMut<Assets<Image>>,
|
||||||
|
) {
|
||||||
|
if atlas.is_loaded {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if asset_server.load_state(atlas.handle.clone()) != LoadState::Loaded {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let image = images.get_mut(&atlas.handle).unwrap();
|
||||||
|
|
||||||
// Create a new array texture asset from the loaded texture.
|
|
||||||
let array_layers = 7;
|
let array_layers = 7;
|
||||||
image.reinterpret_stacked_2d_as_array(array_layers);
|
image.reinterpret_stacked_2d_as_array(array_layers);
|
||||||
|
|
||||||
|
atlas.is_loaded = true;
|
||||||
|
map.ready = true;
|
||||||
|
map.regenerate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
|
fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
|
||||||
@@ -95,13 +117,7 @@ fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo: run after textures are ready
|
fn create_map(mut commands: Commands) {
|
||||||
fn create_map(
|
|
||||||
mut commands: Commands,
|
|
||||||
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
atlas: Res<ChunkAtlas>,
|
|
||||||
) {
|
|
||||||
let heightmap = generate_heightmap(
|
let heightmap = generate_heightmap(
|
||||||
&GenerationConfig {
|
&GenerationConfig {
|
||||||
layers: vec![
|
layers: vec![
|
||||||
@@ -162,6 +178,21 @@ fn create_map(
|
|||||||
2,
|
2,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
commands.insert_resource(heightmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_map(
|
||||||
|
heightmap: Res<Map>,
|
||||||
|
mut commands: Commands,
|
||||||
|
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
atlas: Res<ChunkAtlas>,
|
||||||
|
mut map: ResMut<PhosMap>,
|
||||||
|
) {
|
||||||
|
if !map.ready || !map.regenerate {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
map.regenerate = false;
|
||||||
let chunk_material = materials.add(ExtendedMaterial {
|
let chunk_material = materials.add(ExtendedMaterial {
|
||||||
base: StandardMaterial {
|
base: StandardMaterial {
|
||||||
base_color: Color::WHITE,
|
base_color: Color::WHITE,
|
||||||
@@ -175,21 +206,22 @@ fn create_map(
|
|||||||
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(MaterialMeshBundle {
|
commands.spawn((
|
||||||
mesh: meshes.add(mesh),
|
MaterialMeshBundle {
|
||||||
material: chunk_material.clone(),
|
mesh: meshes.add(mesh),
|
||||||
transform: Transform::from_translation(pos),
|
material: chunk_material.clone(),
|
||||||
..default()
|
transform: Transform::from_translation(pos),
|
||||||
});
|
..default()
|
||||||
|
},
|
||||||
|
PhosChunk,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.insert_resource(heightmap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
||||||
struct ChunkMaterial {
|
struct ChunkMaterial {
|
||||||
#[texture(0, dimension = "2d_array")]
|
#[texture(100, dimension = "2d_array")]
|
||||||
#[sampler(1)]
|
#[sampler(101)]
|
||||||
array_texture: Handle<Image>,
|
array_texture: Handle<Image>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,17 @@
|
|||||||
use bevy::asset::Handle;
|
use bevy::asset::Handle;
|
||||||
use bevy::prelude::{Image, Resource};
|
use bevy::prelude::{Component, Image, Resource};
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct ChunkAtlas {
|
pub struct ChunkAtlas {
|
||||||
pub handle: Handle<Image>,
|
pub handle: Handle<Image>,
|
||||||
|
pub is_loaded: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Default)]
|
||||||
|
pub struct PhosMap {
|
||||||
|
pub ready: bool,
|
||||||
|
pub regenerate: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct PhosChunk;
|
||||||
|
|||||||
Reference in New Issue
Block a user