super hacky texture index

This commit is contained in:
2024-04-07 00:48:22 -04:00
parent 0945ee4023
commit d42f2dd65c
6 changed files with 126 additions and 50 deletions

View File

@@ -2,6 +2,8 @@
pbr_fragment::pbr_input_from_standard_material,
pbr_functions::alpha_discard,
}
#import bevy_pbr::mesh_functions::{mesh_position_local_to_world,get_model_matrix,mesh_normal_local_to_world}
#import bevy_pbr::view_transformations::position_world_to_clip;
#ifdef PREPASS_PIPELINE
#import bevy_pbr::{
@@ -15,19 +17,19 @@
}
#endif
struct ChunkMaterial {
array_texture: texture_2d_array<f32>,
array_texture_sampler: sampler,
}
@group(2) @binding(100)
var<uniform> chunk_material: ChunkMaterial;
@group(2) @binding(100) var array_texture: texture_2d_array<f32>;
@group(2) @binding(101) var array_texture_sampler: sampler;
@fragment
fn fragment(
in: VertexOutput,
@builtin(front_facing) is_front: bool,
) -> FragmentOutput {
// var vin : VertexOutput;
// vin.position = in.position;
// vin.world_position = in.world_position;
// vin.world_normal = in.world_normal;
// vin.uv = in.uv;
// generate a PbrInput struct from the StandardMaterial bindings
var pbr_input = pbr_input_from_standard_material(in, is_front);
@@ -41,10 +43,13 @@ fn fragment(
#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);
let layer = u32(in.uv_b.x);
out.color = textureSample(array_texture, array_texture_sampler, in.uv, layer);
out.color *= apply_pbr_lighting(pbr_input);
// 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.
@@ -55,4 +60,31 @@ fn fragment(
#endif
return out;
}
}
//struct Vertex {
// @builtin(instance_index) instance_index: u32,
// @location(0) position: vec3<f32>,
// @location(1) uv: vec2<f32>,
// @location(2) normal: vec3<f32>,
// @location(3) texture_index: u32,
//};
//
//struct VOut {
// @builtin(position) position: vec4<f32>,
// @location(0) world_position: vec4<f32>,
// @location(1) world_normal: vec3<f32>,
// @location(2) uv: vec2<f32>,
//// @location(7) @interpolate(flat) texture_index: u32,
//};
//
//@vertex
//fn vertex(vertex: Vertex) -> VOut {
// var out: VOut;
// out.world_position = mesh_position_local_to_world(get_model_matrix(vertex.instance_index), vec4<f32>(vertex.position, 1.0));
// out.position = position_world_to_clip(out.world_position.xyz);
//// out.texture_index = vertex.texture_index;
// out.uv = vertex.uv;
// out.world_normal = mesh_normal_local_to_world(vertex.normal, vertex.instance_index);
// return out;
//}

View File

@@ -1,6 +1,8 @@
use bevy::prelude::*;
use bevy::render::texture::{ImageAddressMode, ImageFilterMode, ImageSamplerDescriptor};
use bevy::window::PresentMode;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
mod phos;
mod prelude;
@@ -19,6 +21,13 @@ fn main() {
..default()
}),
..default()
}).set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Nearest,
..default()
}
}),
WorldInspectorPlugin::new(),
PhosGamePlugin,

View File

@@ -1,6 +1,5 @@
use bevy::asset::LoadState;
use bevy::pbr::{ExtendedMaterial, MaterialExtension};
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::pbr::{ExtendedMaterial};
use bevy::{pbr::CascadeShadowConfig, prelude::*};
use camera_system::PhosCameraPlugin;
use iyes_perf_ui::prelude::*;
@@ -8,7 +7,6 @@ use world_generation::hex_utils::{offset_to_world, HexCoord};
use world_generation::{
heightmap::generate_heightmap, mesh_generator::generate_chunk_mesh, prelude::*,
};
use crate::prelude::*;
pub struct PhosGamePlugin;
@@ -82,6 +80,7 @@ fn check_texture(
let array_layers = 7;
image.reinterpret_stacked_2d_as_array(array_layers);
atlas.is_loaded = true;
map.ready = true;
map.regenerate = true;
@@ -175,7 +174,7 @@ fn create_map(mut commands: Commands) {
border_size: 64.,
size: UVec2::splat(1024 / Chunk::SIZE as u32),
},
2,
4,
);
commands.insert_resource(heightmap);
@@ -218,15 +217,3 @@ fn spawn_map(
}
}
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
struct ChunkMaterial {
#[texture(100, dimension = "2d_array")]
#[sampler(101)]
array_texture: Handle<Image>,
}
impl MaterialExtension for ChunkMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/world/chunk.wgsl".into()
}
}

View File

@@ -1,5 +1,9 @@
use bevy::asset::Handle;
use bevy::prelude::{Component, Image, Resource};
use bevy::asset::{Asset, Handle};
use bevy::pbr::{MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline, MaterialPipeline, MaterialPipelineKey};
use bevy::prelude::{Component, Image, Mesh, Resource, TypePath};
use bevy::render::mesh::{Indices, MeshVertexBufferLayout};
use bevy::render::render_resource::{AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError};
use world_generation::prelude::ATTRIBUTE_TEXTURE_INDEX;
#[derive(Resource)]
pub struct ChunkAtlas {
@@ -15,3 +19,33 @@ pub struct PhosMap {
#[derive(Component)]
pub struct PhosChunk;
#[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(3),
// ])?;
// descriptor.vertex.buffers = vec![vertex_layout];
// Ok(())
// }
}