Skip to content

Commit

Permalink
Added ARRAY_TEXTURES support to shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
IamTheCarl committed Dec 3, 2022
1 parent 5718a7e commit 49db5d0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_pbr/src/render/mesh_vertex_output.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
#ifdef VERTEX_COLORS
@location(4) color: vec4<f32>,
#endif
#ifdef ARRAY_TEXTURES
@location(5) texture_layer: i32,
#endif
28 changes: 24 additions & 4 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
#endif
#ifdef VERTEX_UVS
if ((material.flags & STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u) {
output_color = output_color * textureSample(base_color_texture, base_color_sampler, in.uv);
output_color = output_color * textureSample(base_color_texture, base_color_sampler, in.uv
#ifdef ARRAY_TEXTURES
, in.texture_layer
#endif
);
}
#endif

Expand All @@ -41,7 +45,12 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
var emissive: vec4<f32> = material.emissive;
#ifdef VERTEX_UVS
if ((material.flags & STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT) != 0u) {
emissive = vec4<f32>(emissive.rgb * textureSample(emissive_texture, emissive_sampler, in.uv).rgb, 1.0);
emissive = vec4<f32>(emissive.rgb * textureSample(emissive_texture, emissive_sampler, in.uv
#ifdef ARRAY_TEXTURES
, in.texture_layer
#endif
).rgb, 1.0
);
}
#endif
pbr_input.material.emissive = emissive;
Expand All @@ -50,7 +59,11 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
var perceptual_roughness: f32 = material.perceptual_roughness;
#ifdef VERTEX_UVS
if ((material.flags & STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT) != 0u) {
let metallic_roughness = textureSample(metallic_roughness_texture, metallic_roughness_sampler, in.uv);
let metallic_roughness = textureSample(metallic_roughness_texture, metallic_roughness_sampler, in.uv
#ifdef ARRAY_TEXTURES
, in.texture_layer
#endif
);
// Sampling from GLTF standard channels for now
metallic = metallic * metallic_roughness.b;
perceptual_roughness = perceptual_roughness * metallic_roughness.g;
Expand All @@ -62,7 +75,11 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
var occlusion: f32 = 1.0;
#ifdef VERTEX_UVS
if ((material.flags & STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT) != 0u) {
occlusion = textureSample(occlusion_texture, occlusion_sampler, in.uv).r;
occlusion = textureSample(occlusion_texture, occlusion_sampler, in.uv
#ifdef ARRAY_TEXTURES
, in.texture_layer
#endif
).r;
}
#endif
pbr_input.occlusion = occlusion;
Expand All @@ -87,6 +104,9 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
#endif
#ifdef VERTEX_UVS
in.uv,
#endif
#ifdef ARRAY_TEXTURES
in.texture_layer,
#endif
);
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);
Expand Down
24 changes: 24 additions & 0 deletions crates/bevy_pbr/src/render/pbr_bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

@group(1) @binding(0)
var<uniform> material: StandardMaterial;

#ifndef ARRAY_TEXTURES
@group(1) @binding(1)
var base_color_texture: texture_2d<f32>;
@group(1) @binding(2)
Expand All @@ -24,3 +26,25 @@ var occlusion_sampler: sampler;
var normal_map_texture: texture_2d<f32>;
@group(1) @binding(10)
var normal_map_sampler: sampler;
#else
@group(1) @binding(1)
var base_color_texture: texture_2d_array<f32>;
@group(1) @binding(2)
var base_color_sampler: sampler;
@group(1) @binding(3)
var emissive_texture: texture_2d_array<f32>;
@group(1) @binding(4)
var emissive_sampler: sampler;
@group(1) @binding(5)
var metallic_roughness_texture: texture_2d_array<f32>;
@group(1) @binding(6)
var metallic_roughness_sampler: sampler;
@group(1) @binding(7)
var occlusion_texture: texture_2d_array<f32>;
@group(1) @binding(8)
var occlusion_sampler: sampler;
@group(1) @binding(9)
var normal_map_texture: texture_2d_array<f32>;
@group(1) @binding(10)
var normal_map_sampler: sampler;
#endif
9 changes: 8 additions & 1 deletion crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ fn apply_normal_mapping(
#ifdef VERTEX_UVS
uv: vec2<f32>,
#endif
#ifdef ARRAY_TEXTURES
texture_layer: i32,
#endif
) -> vec3<f32> {
// NOTE: The mikktspace method of normal mapping explicitly requires that the world normal NOT
// be re-normalized in the fragment shader. This is primarily to match the way mikktspace
Expand All @@ -74,7 +77,11 @@ fn apply_normal_mapping(
#ifdef VERTEX_UVS
#ifdef STANDARDMATERIAL_NORMAL_MAP
// Nt is the tangent-space normal.
var Nt = textureSample(normal_map_texture, normal_map_sampler, uv).rgb;
var Nt = textureSample(normal_map_texture, normal_map_sampler, uv
#ifdef ARRAY_TEXTURES
, texture_layer
#endif
).rgb;
if ((standard_material_flags & STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP) != 0u) {
// Only use the xy components and derive z for 2-component normal maps.
Nt = vec3<f32>(Nt.rg * 2.0 - 1.0, 0.0);
Expand Down

0 comments on commit 49db5d0

Please sign in to comment.