Skip to content

Commit ad5d790

Browse files
authored
Fix WebGL not rendering StandardMaterial (#12110)
# Objective - Fixes #12081 ## Solution Passing the `Affine2` as a neatly packed `mat3x2` breaks WebGL with `drawElementsInstanced: Buffer for uniform block is smaller than UNIFORM_BLOCK_DATA_SIZE.` I fixed this by using a `mat3x3` instead. Alternative solutions that come to mind: - Pass in a `mat3x2` on non-webgl targets and a `mat3x3` otherwise. I guess I could use `#ifdef SIXTEEN_BYTE_ALIGNMENT` for this, but it doesn't seem quite right? This would be more efficient, but decrease code quality. - Do something about `UNIFORM_BLOCK_DATA_SIZE`. I don't know how, so I'd need some guidance here. @superdump let me know if you'd like me to implement other variants. Otherwise, I vote for merging this as a quick fix for `main` and then improving the packing in subsequent PRs :) ## Additional notes Ideally we should merge this before @JMS55 rebases #10164 so that they don't have to rebase everything a second time.
1 parent 5e63f68 commit ad5d790

File tree

4 files changed

+8
-16
lines changed

4 files changed

+8
-16
lines changed

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bevy_asset::Asset;
2-
use bevy_math::{Affine2, Vec2, Vec4};
2+
use bevy_math::{Affine2, Mat3, Vec4};
33
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
44
use bevy_render::{mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_resource::*};
55

@@ -596,12 +596,8 @@ pub struct StandardMaterialUniform {
596596
pub emissive: Vec4,
597597
/// Color white light takes after travelling through the attenuation distance underneath the material surface
598598
pub attenuation_color: Vec4,
599-
/// The x-axis of the mat2 of the transform applied to the UVs corresponding to ATTRIBUTE_UV_0 on the mesh before sampling. Default is [1, 0].
600-
pub uv_transform_x_axis: Vec2,
601-
/// The y-axis of the mat2 of the transform applied to the UVs corresponding to ATTRIBUTE_UV_0 on the mesh before sampling. Default is [0, 1].
602-
pub uv_transform_y_axis: Vec2,
603-
/// The translation of the transform applied to the UVs corresponding to ATTRIBUTE_UV_0 on the mesh before sampling. Default is [0, 0].
604-
pub uv_transform_translation: Vec2,
599+
/// The transform applied to the UVs corresponding to ATTRIBUTE_UV_0 on the mesh before sampling. Default is identity.
600+
pub uv_transform: Mat3,
605601
/// Linear perceptual roughness, clamped to [0.089, 1.0] in the shader
606602
/// Defaults to minimum of 0.089
607603
pub roughness: f32,
@@ -736,9 +732,7 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
736732
lightmap_exposure: self.lightmap_exposure,
737733
max_relief_mapping_search_steps: self.parallax_mapping_method.max_steps(),
738734
deferred_lighting_pass_id: self.deferred_lighting_pass_id as u32,
739-
uv_transform_x_axis: self.uv_transform.matrix2.x_axis,
740-
uv_transform_y_axis: self.uv_transform.matrix2.y_axis,
741-
uv_transform_translation: self.uv_transform.translation,
735+
uv_transform: self.uv_transform.into(),
742736
}
743737
}
744738
}

crates/bevy_pbr/src/render/pbr_fragment.wgsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
parallax_mapping::parallaxed_uv,
1212
lightmap::lightmap,
1313
}
14-
#import bevy_render::maths::affine2_to_square
1514

1615
#ifdef SCREEN_SPACE_AMBIENT_OCCLUSION
1716
#import bevy_pbr::mesh_view_bindings::screen_space_ambient_occlusion_texture
@@ -74,7 +73,7 @@ fn pbr_input_from_standard_material(
7473
let NdotV = max(dot(pbr_input.N, pbr_input.V), 0.0001);
7574

7675
#ifdef VERTEX_UVS
77-
let uv_transform = affine2_to_square(pbr_bindings::material.uv_transform);
76+
let uv_transform = pbr_bindings::material.uv_transform;
7877
var uv = (uv_transform * vec3(in.uv, 1.0)).xy;
7978

8079
#ifdef VERTEX_TANGENTS

crates/bevy_pbr/src/render/pbr_prepass_functions.wgsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
pbr_bindings,
88
pbr_types,
99
}
10-
#import bevy_render::maths::affine2_to_square
1110

1211
// Cutoff used for the premultiplied alpha modes BLEND and ADD.
1312
const PREMULTIPLIED_ALPHA_CUTOFF = 0.05;
@@ -19,7 +18,7 @@ fn prepass_alpha_discard(in: VertexOutput) {
1918
var output_color: vec4<f32> = pbr_bindings::material.base_color;
2019

2120
#ifdef VERTEX_UVS
22-
let uv_transform = affine2_to_square(pbr_bindings::material.uv_transform);
21+
let uv_transform = pbr_bindings::material.uv_transform;
2322
let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
2423
if (pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u {
2524
output_color = output_color * textureSampleBias(pbr_bindings::base_color_texture, pbr_bindings::base_color_sampler, uv, view.mip_bias);

crates/bevy_pbr/src/render/pbr_types.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct StandardMaterial {
66
base_color: vec4<f32>,
77
emissive: vec4<f32>,
88
attenuation_color: vec4<f32>,
9-
uv_transform: mat3x2<f32>,
9+
uv_transform: mat3x3<f32>,
1010
perceptual_roughness: f32,
1111
metallic: f32,
1212
reflectance: f32,
@@ -78,7 +78,7 @@ fn standard_material_new() -> StandardMaterial {
7878
material.max_relief_mapping_search_steps = 5u;
7979
material.deferred_lighting_pass_id = 1u;
8080
// scale 1, translation 0, rotation 0
81-
material.uv_transform = mat3x2<f32>(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
81+
material.uv_transform = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
8282

8383
return material;
8484
}

0 commit comments

Comments
 (0)