Skip to content

Commit

Permalink
add: texture scale in terrain shader as defined in textures_set
Browse files Browse the repository at this point in the history
  • Loading branch information
Sufhal committed Sep 21, 2024
1 parent c3e904d commit c30df62
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
12 changes: 6 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ fn main() -> anyhow::Result<()> {
// This tells Cargo to rerun this script if something in /assets/ changes.
// println!("cargo:rerun-if-changed=assets/*");

let out_dir = env::var("OUT_DIR")?;
let mut copy_options = CopyOptions::new();
copy_options.overwrite = true;
let mut paths_to_copy = Vec::new();
paths_to_copy.push("assets/");
copy_items(&paths_to_copy, out_dir, &copy_options)?;
// let out_dir = env::var("OUT_DIR")?;
// let mut copy_options = CopyOptions::new();
// copy_options.overwrite = true;
// let mut paths_to_copy = Vec::new();
// paths_to_copy.push("assets/");
// copy_items(&paths_to_copy, out_dir, &copy_options)?;

Ok(())
}
Binary file modified pkg/m2rs_bg.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion src/modules/geometry/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Plane {
textures: &Vec<Texture>,
alpha_atlas: &Texture,
textures_set: &ChunkTextureSet,
textures_scale: Vec<f32>,
) -> CustomMesh {
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Terrain Vertex Buffer"),
Expand All @@ -158,7 +159,7 @@ impl Plane {
});
let chunk_informations_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Chunk Informations Buffer"),
contents: bytemuck::cast_slice(&[ChunkInformationUniform { textures_count: textures_set.textures.len() as u32 }]),
contents: bytemuck::cast_slice(&[ChunkInformationUniform::new(textures_set.textures.len() as u32, &textures_scale)]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});

Expand Down
40 changes: 35 additions & 5 deletions src/modules/terrain/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cgmath::{Matrix4, Rotation3};
use wgpu::util::DeviceExt;
use crate::modules::{assets::assets::load_binary, core::{model::{CustomMesh, Transformable}, object_3d::Object3D, texture::Texture}, geometry::plane::Plane, state::State, utils::functions::u8_to_string_with_len};
use super::{areadata::AreaData, height::Height, setting::Setting, texture_set::ChunkTextureSet, water::{Water, WaterTexture}};
use super::{areadata::AreaData, height::Height, setting::Setting, texture_set::{ChunkTextureSet, TextureSet}, water::{Water, WaterTexture}};

const SIZE: f32 = 256.0;

Expand All @@ -24,7 +24,8 @@ impl Chunk {
y: &u8,
setting: &Setting,
textures: &Vec<Texture>,
water_textures: &WaterTexture,
terrain_textures_set: &TextureSet,
water_texture: &WaterTexture,
state: &mut State<'_>
) -> anyhow::Result<Self> {
let name = Self::name_from(*x, *y);
Expand All @@ -33,6 +34,7 @@ impl Chunk {
let mean_height = height.vertices.iter().fold(0.0, |acc, v| acc + *v) / height.vertices.len() as f32;
let water = Water::read(&chunk_path).await?;
let textures_set = ChunkTextureSet::read(&chunk_path).await?;
let textures_scale = textures_set.textures.iter().map(|idx| terrain_textures_set.definitions[*idx as usize].u_scale).collect::<Vec<_>>();
let alpha_atlas = {
let raw = load_binary(&format!("{chunk_path}/tiles_atlas.raw")).await?;
Texture::from_raw_bytes(
Expand Down Expand Up @@ -62,7 +64,8 @@ impl Chunk {
name.clone(),
textures,
&alpha_atlas,
&textures_set
&textures_set,
textures_scale
);
let water_plane = water.generate_plane(setting.height_scale);
let depth = Water::calculate_depth(&water_plane, &terrain_plane);
Expand All @@ -80,7 +83,7 @@ impl Chunk {
0.0,
(*y as f32 * size)
],
&water_textures,
&water_texture,
);
let area_data = AreaData::read(&chunk_path).await?;
let limits = (
Expand Down Expand Up @@ -159,5 +162,32 @@ impl Chunk {
#[repr(C)]
#[derive(bytemuck::Pod, bytemuck::Zeroable, Copy, Clone)]
pub struct ChunkInformationUniform {
pub textures_count: u32
textures_count: [u32; 4],
textures_scale_1: [f32; 4],
textures_scale_2: [f32; 4],
}

impl ChunkInformationUniform {
pub fn new(textures_count: u32, textures_scale: &Vec<f32>) -> Self {
Self {
textures_count: [
textures_count,
0,
0,
0
],
textures_scale_1: [
*textures_scale.get(0).unwrap_or(&1.0),
*textures_scale.get(1).unwrap_or(&1.0),
*textures_scale.get(2).unwrap_or(&1.0),
*textures_scale.get(3).unwrap_or(&1.0),
],
textures_scale_2: [
*textures_scale.get(4).unwrap_or(&1.0),
*textures_scale.get(5).unwrap_or(&1.0),
*textures_scale.get(6).unwrap_or(&1.0),
*textures_scale.get(7).unwrap_or(&1.0),
]
}
}
}
5 changes: 3 additions & 2 deletions src/modules/terrain/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Terrain {
pub async fn load(name: &str, state: &mut State<'_>) -> anyhow::Result<Self> {
let path = format!("pack/map/{name}");
let setting = Setting::read(&path).await?;
let texture_set = TextureSet::read(&path).await?;
let terrain_textures_set = TextureSet::read(&path).await?;
let water_texture = WaterTexture::load(state).await?;
let textures = texture_set.load_textures(&state.device, &state.queue).await?;
let textures = terrain_textures_set.load_textures(&state.device, &state.queue).await?;
let mut chunks = Vec::new();
for x in 0..setting.map_size[0] {
for y in 0..setting.map_size[1] {
Expand All @@ -27,6 +27,7 @@ impl Terrain {
&y,
&setting,
&textures,
&terrain_textures_set,
&water_texture,
state
).await?;
Expand Down
25 changes: 13 additions & 12 deletions src/shaders/terrain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ struct DirectionalLight {
@group(0) @binding(7) var<uniform> directional_light: DirectionalLight;

struct ChunkInformations {
textures_count: u32,
textures_count: vec4<u32>,
textures_scale_1: vec4<f32>,
textures_scale_2: vec4<f32>,
}
@group(1) @binding(1) var<uniform> chunk_informations: ChunkInformations;
@group(1) @binding(2) var sampler_tex: sampler;
Expand Down Expand Up @@ -173,17 +175,16 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var splat = vec4<f32>(0.0, 0.0, 0.0, 1.0);

let uv = in.tex_coords;
let tex_uv = uv * 40.0; // TODO: map level textureset.json contains data about this factor
let t0 = textureSample(tex_0, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 0u, vec2<u32>(3, 3))).r;
let t1 = textureSample(tex_1, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 1u, vec2<u32>(3, 3))).r;
let t2 = textureSample(tex_2, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 2u, vec2<u32>(3, 3))).r;
let t3 = textureSample(tex_3, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 3u, vec2<u32>(3, 3))).r;
let t4 = textureSample(tex_4, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 4u, vec2<u32>(3, 3))).r;
let t5 = textureSample(tex_5, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 5u, vec2<u32>(3, 3))).r;
let t6 = textureSample(tex_6, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 6u, vec2<u32>(3, 3))).r;
let t7 = textureSample(tex_7, sampler_tex, tex_uv) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 7u, vec2<u32>(3, 3))).r;

for (var i: u32 = 0; i < chunk_informations.textures_count; i = i + 1) {
let t0 = textureSample(tex_0, sampler_tex, uv * chunk_informations.textures_scale_1[0] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 0u, vec2<u32>(3, 3))).r;
let t1 = textureSample(tex_1, sampler_tex, uv * chunk_informations.textures_scale_1[1] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 1u, vec2<u32>(3, 3))).r;
let t2 = textureSample(tex_2, sampler_tex, uv * chunk_informations.textures_scale_1[2] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 2u, vec2<u32>(3, 3))).r;
let t3 = textureSample(tex_3, sampler_tex, uv * chunk_informations.textures_scale_1[3] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 3u, vec2<u32>(3, 3))).r;
let t4 = textureSample(tex_4, sampler_tex, uv * chunk_informations.textures_scale_2[0] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 4u, vec2<u32>(3, 3))).r;
let t5 = textureSample(tex_5, sampler_tex, uv * chunk_informations.textures_scale_2[1] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 5u, vec2<u32>(3, 3))).r;
let t6 = textureSample(tex_6, sampler_tex, uv * chunk_informations.textures_scale_2[2] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 6u, vec2<u32>(3, 3))).r;
let t7 = textureSample(tex_7, sampler_tex, uv * chunk_informations.textures_scale_2[3] * 10.0) * textureSample(tex_alpha_atlas, sampler_alpha, get_uv_in_atlas(uv, 7u, vec2<u32>(3, 3))).r;

for (var i: u32 = 0; i < chunk_informations.textures_count[0]; i = i + 1) {
switch i {
case 0u: {
splat += t0;
Expand Down

0 comments on commit c30df62

Please sign in to comment.