Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1// Since this is a hot path, try to keep the alignment and size of the struct members in mind.
2// You can find the alignment and sizes at <https://www.w3.org/TR/WGSL/#alignment-and-size>.
3struct StandardMaterial {
4 base_color: vec4<f32>,
5 emissive: vec4<f32>,
6 attenuation_color: vec4<f32>,
7 uv_transform: mat3x3<f32>,
8 reflectance: vec3<f32>,
9 perceptual_roughness: f32,
10 metallic: f32,
11 diffuse_transmission: f32,
12 specular_transmission: f32,
13 thickness: f32,
14 ior: f32,
15 attenuation_distance: f32,
16 clearcoat: f32,
17 clearcoat_perceptual_roughness: f32,
18 anisotropy_strength: f32,
19 anisotropy_rotation: vec2<f32>,
20 /// 'flags' is a bit field indicating various options. u32 is 32 bits so we have up to 32 options.
21 flags: u32,
22 alpha_cutoff: f32,
23 parallax_depth_scale: f32,
24 max_parallax_layer_count: f32,
25 lightmap_exposure: f32,
26 max_relief_mapping_search_steps: u32,
27 /// ID for specifying which deferred lighting pass should be used for rendering this material, if any.
28 deferred_lighting_pass_id: u32,
29};
31// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32// NOTE: if these flags are updated or changed. Be sure to also update
33// deferred_flags_from_mesh_material_flags and mesh_material_flags_from_deferred_flags
34// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35const STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u << 0u;
36const STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT: u32 = 1u << 1u;
37const STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 1u << 2u;
38const STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 1u << 3u;
39const STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 1u << 4u;
40const STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 1u << 5u;
41const STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 1u << 6u;
42const STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 1u << 7u;
43const STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT: u32 = 1u << 8u;
44const STANDARD_MATERIAL_FLAGS_DEPTH_MAP_BIT: u32 = 1u << 9u;
45const STANDARD_MATERIAL_FLAGS_SPECULAR_TRANSMISSION_TEXTURE_BIT: u32 = 1u << 10u;
46const STANDARD_MATERIAL_FLAGS_THICKNESS_TEXTURE_BIT: u32 = 1u << 11u;
47const STANDARD_MATERIAL_FLAGS_DIFFUSE_TRANSMISSION_TEXTURE_BIT: u32 = 1u << 12u;
48const STANDARD_MATERIAL_FLAGS_ATTENUATION_ENABLED_BIT: u32 = 1u << 13u;
49const STANDARD_MATERIAL_FLAGS_CLEARCOAT_TEXTURE_BIT: u32 = 1u << 14u;
50const STANDARD_MATERIAL_FLAGS_CLEARCOAT_ROUGHNESS_TEXTURE_BIT: u32 = 1u << 15u;
51const STANDARD_MATERIAL_FLAGS_CLEARCOAT_NORMAL_TEXTURE_BIT: u32 = 1u << 16u;
52const STANDARD_MATERIAL_FLAGS_ANISOTROPY_TEXTURE_BIT: u32 = 1u << 17u;
53const STANDARD_MATERIAL_FLAGS_SPECULAR_TEXTURE_BIT: u32 = 1u << 18u;
54const STANDARD_MATERIAL_FLAGS_SPECULAR_TINT_TEXTURE_BIT: u32 = 1u << 19u;
55const STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_RG_BIT: u32 = 1u << 20u;
56const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 7u << 29u; // (0b111u << 29u)
57const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u << 29u;
58const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 1u << 29u;
59const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 2u << 29u;
60const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 3u << 29u;
61const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 4u << 29u;
62const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 5u << 29u;
63const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ALPHA_TO_COVERAGE: u32 = 6u << 29u;
66/// Creates a StandardMaterial with default values
67fn standard_material_new() -> StandardMaterial {
68 var material: StandardMaterial;
70 // NOTE: Keep in-sync with src/pbr_material.rs!
71 material.base_color = vec4<f32>(1.0, 1.0, 1.0, 1.0);
72 material.emissive = vec4<f32>(0.0, 0.0, 0.0, 1.0);
73 material.perceptual_roughness = 0.5;
74 material.metallic = 0.00;
75 material.reflectance = vec3<f32>(0.5);
76 material.diffuse_transmission = 0.0;
77 material.specular_transmission = 0.0;
78 material.thickness = 0.0;
79 material.ior = 1.5;
80 material.attenuation_distance = 1.0;
81 material.attenuation_color = vec4<f32>(1.0, 1.0, 1.0, 1.0);
82 material.clearcoat = 0.0;
83 material.clearcoat_perceptual_roughness = 0.0;
84 material.flags = STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE;
85 material.alpha_cutoff = 0.5;
86 material.parallax_depth_scale = 0.1;
87 material.max_parallax_layer_count = 16.0;
88 material.max_relief_mapping_search_steps = 5u;
89 material.deferred_lighting_pass_id = 1u;
90 // scale 1, translation 0, rotation 0
91 material.uv_transform = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
93 return material;
96struct PbrInput {
97 material: StandardMaterial,
98 /// Note: this gets monochromized upon deferred PbrInput reconstruction.
99 diffuse_occlusion: vec3<f32>,
100 /// Note: this is 1.0 (entirely unoccluded) when SSAO and SSR are off.
101 specular_occlusion: f32,
102 frag_coord: vec4<f32>,
103 world_position: vec4<f32>,
104 /// Normalized world normal used for shadow mapping as normal-mapping is not used for shadow
105 /// mapping
106 world_normal: vec3<f32>,
107 /// Normalized normal-mapped world normal used for lighting
108 N: vec3<f32>,
109 /// Normalized view vector in world space, pointing from the fragment world position toward the
110 /// view world position
111 V: vec3<f32>,
112 lightmap_light: vec3<f32>,
113 clearcoat_N: vec3<f32>,
114 anisotropy_strength: f32,
115 /// These two aren't specific to anisotropy, but we only fill them in if
116 /// we're doing anisotropy, so they're prefixed with `anisotropy_`.
117 anisotropy_T: vec3<f32>,
118 anisotropy_B: vec3<f32>,
119 is_orthographic: bool,
120 flags: u32,
121 /// Multiplier applied to all directional lights before cascade and contact shadows,
122 /// use this for custom shadow maps. Defaults to 1.0.
123 directional_shadow_factor: f32,
124};
126/// Creates a PbrInput with default values
127fn pbr_input_new() -> PbrInput {
128 var pbr_input: PbrInput;
130 pbr_input.material = standard_material_new();
131 pbr_input.diffuse_occlusion = vec3<f32>(1.0);
132 // If SSAO is enabled, then this gets overwritten with proper specular occlusion. If its not, then we get specular environment map unoccluded (we have no data with which to occlude it with).
133 pbr_input.specular_occlusion = 1.0;
135 pbr_input.frag_coord = vec4<f32>(0.0, 0.0, 0.0, 1.0);
136 pbr_input.world_position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
137 pbr_input.world_normal = vec3<f32>(0.0, 0.0, 1.0);
139 pbr_input.is_orthographic = false;
141 pbr_input.N = vec3<f32>(0.0, 0.0, 1.0);
142 pbr_input.V = vec3<f32>(1.0, 0.0, 0.0);
144 pbr_input.clearcoat_N = vec3<f32>(0.0);
145 pbr_input.anisotropy_T = vec3<f32>(0.0);
146 pbr_input.anisotropy_B = vec3<f32>(0.0);
148 pbr_input.lightmap_light = vec3<f32>(0.0);
150 pbr_input.flags = 0u;
151 pbr_input.directional_shadow_factor = 1.0;
153 return pbr_input;
Contents