Skip to content

Commit

Permalink
mat4?
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Feb 28, 2024
1 parent fbcdfb9 commit d0221a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
14 changes: 11 additions & 3 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_asset::Asset;
use bevy_math::{Affine2, Mat3, Vec4};
use bevy_math::{Affine2, Mat3, Mat4, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_resource::*};

Expand Down Expand Up @@ -597,7 +597,7 @@ pub struct StandardMaterialUniform {
/// Color white light takes after travelling through the attenuation distance underneath the material surface
pub attenuation_color: Vec4,
/// The transform applied to the UVs corresponding to ATTRIBUTE_UV_0 on the mesh before sampling. Default is identity.
pub uv_transform: Mat3,
pub uv_transform: Mat4,
/// Linear perceptual roughness, clamped to [0.089, 1.0] in the shader
/// Defaults to minimum of 0.089
pub roughness: f32,
Expand Down Expand Up @@ -713,6 +713,14 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
flags |= StandardMaterialFlags::ATTENUATION_ENABLED;
}

let a: Mat3 = self.uv_transform.into();
let uv = Mat4 {
x_axis: a.x_axis.extend(0.0),
y_axis: a.y_axis.extend(0.0),
z_axis: a.z_axis.extend(0.0),
w_axis: Vec4::ZERO,
};

StandardMaterialUniform {
base_color: self.base_color.as_linear_rgba_f32().into(),
emissive: self.emissive.as_linear_rgba_f32().into(),
Expand All @@ -732,7 +740,7 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
lightmap_exposure: self.lightmap_exposure,
max_relief_mapping_search_steps: self.parallax_mapping_method.max_steps(),
deferred_lighting_pass_id: self.deferred_lighting_pass_id as u32,
uv_transform: self.uv_transform.into(),
uv_transform: uv,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/render/pbr_fragment.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fn pbr_input_from_standard_material(
let NdotV = max(dot(pbr_input.N, pbr_input.V), 0.0001);

#ifdef VERTEX_UVS
let uv_transform = pbr_bindings::material.uv_transform;
let zut = mat3x3(pbr_bindings::material.uv_transform[0].xyz, pbr_bindings::material.uv_transform[1].xyz, pbr_bindings::material.uv_transform[2].xyz);
let uv_transform = zut;
var uv = (uv_transform * vec3(in.uv, 1.0)).xy;

#ifdef VERTEX_TANGENTS
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/render/pbr_prepass_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn prepass_alpha_discard(in: VertexOutput) {
var output_color: vec4<f32> = pbr_bindings::material.base_color;

#ifdef VERTEX_UVS
let uv_transform = pbr_bindings::material.uv_transform;
let zut = mat3x3(pbr_bindings::material.uv_transform[0].xyz, pbr_bindings::material.uv_transform[1].xyz, pbr_bindings::material.uv_transform[2].xyz);
let uv_transform = zut;
let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
if (pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, uv, view.mip_bias);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/pbr_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct StandardMaterial {
base_color: vec4<f32>,
emissive: vec4<f32>,
attenuation_color: vec4<f32>,
uv_transform: mat3x3<f32>,
uv_transform: mat4x4<f32>,
perceptual_roughness: f32,
metallic: f32,
reflectance: f32,
Expand Down Expand Up @@ -78,7 +78,7 @@ fn standard_material_new() -> StandardMaterial {
material.max_relief_mapping_search_steps = 5u;
material.deferred_lighting_pass_id = 1u;
// scale 1, translation 0, rotation 0
material.uv_transform = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
material.uv_transform = mat4x4<f32>(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);

return material;
}
Expand Down

0 comments on commit d0221a1

Please sign in to comment.