11#include " Alimer.hlsli"
2- #include " VertexInputOutput .hlsli"
2+ #include " BRDF .hlsli"
33#include " ShaderTypes.h"
4+ #include " VertexInputOutput.hlsli"
45
56struct SurfaceInfo
67{
@@ -25,10 +26,51 @@ struct SurfaceInfo
2526 float3 F0;
2627};
2728
29+ float3 getDiffuseLightColor(in float3 N)
30+ {
31+ // TODO
32+ // float width, height, numberOfLevels;
33+ // environmentTexture.GetDimensions(0, width, height, numberOfLevels);
34+
35+ // const float diffuseLevel = float(numberOfLevels - 1);
36+ // return environmentTexture.SampleLevel(environmentSampler, N, diffuseLevel).rgb;
37+ return float3(1 . 0 f , 1 . 0 f , 1 . 0 f );
38+ }
39+
40+ float3 getSpecularLightColor(in float3 R, in float roughness)
41+ {
42+ // TODO
43+ // float width, height, numberOfLevels;
44+ // environmentTexture.GetDimensions(0, width, height, numberOfLevels);
45+ //
46+ // const float rough = numberOfLevels * roughness * (2.0f - roughness);
47+ // return environmentTexture.SampleLevel(environmentSampler, R, rough).rgb;
48+ return float3(1 . 0 f , 1 . 0 f , 1 . 0 f );
49+ }
50+
51+ float3 pbrSurfaceColorIbl(in SurfaceInfo surface)
52+ {
53+ float3 kS = FresnelSchlickRoughness(surface .NdotV , surface .F0 , surface .roughness );
54+ float3 kD = (float3(1 . f , 1 . f , 1 . f ) - kS) * (1 . f - surface .metalness );
55+ float3 irradiance = getDiffuseLightColor(surface .N );
56+ float3 diffuse = irradiance * surface .diffuseColor ;
57+
58+ float3 prefilteredColor = getSpecularLightColor(surface .R , surface .roughness );
59+ float2 envBrdf = envBRDFApprox(surface .roughness , surface .NdotV );
60+ float3 specular = prefilteredColor * (surface .specularColor * envBrdf .x + envBrdf .y );
61+
62+ float3 ambient = (kD * diffuse + specular) * surface .ao ;
63+ return ambient;
64+ }
65+
2866// Material (space 0)
29- ConstantBuffer < PBRMaterialData > material : register (b0);
67+ ConstantBuffer < PBRMaterialUniforms > material : register (b0);
3068Texture2D < float4 > baseColorTexture : register (t0);
3169Texture2D < float4 > normalTexture : register (t1);
70+ Texture2D < float4 > metallicRoughnessTexture : register (t2);
71+ Texture2D < float4 > emissiveTexture : register (t3);
72+ Texture2D < float4 > occlusionTexture : register (t4);
73+ // Texture2D<float4> transmissionTexture : register(t5, space1);
3274SamplerState pbrSampler : register (s0);
3375
3476[shader(" pixel" )]
@@ -52,13 +94,59 @@ float4 fragmentMain(in VertexOutput input, in bool isFrontFace: SV_IsFrontFace)
5294 {
5395 float3 N = float3(normalTexture .Sample (pbrSampler, input .TexCoord0 ).rg , 1 . f );
5496 N = N * 2 . f - 1 . f ;
55- // N.rg *= material.GetNormalMapStrength();
97+ // N.rg *= material.GetNormalMapStrength();
5698 surface .N = normalize(mul(N, TBN));
5799 if (! isFrontFace) // MATERIAL_DOUBLE_SIDED?
58100 {
59101 surface .N = - surface .N ;
60102 }
61103 }
62104
63- return baseColor;
105+ surface .NdotV = saturate(dot(surface .N , surface .V ));
106+ surface .R = reflect(- surface .V , surface .N );
107+
108+ const float3 color = input .Color * baseColor .rgb ;
109+ surface .alpha = baseColor .a ;
110+ surface .ao = material .occlusionStrength * occlusionTexture .Sample (pbrSampler, input .TexCoord0 ).r ;
111+ surface .emissive = material .emissiveFactor * emissiveTexture .Sample (pbrSampler, input .TexCoord0 ).rgb ;
112+
113+ const float4 metallicRoughnessMap = metallicRoughnessTexture .Sample (pbrSampler, input .TexCoord0 );
114+ const float4 occlusionMap = occlusionTexture .Sample (pbrSampler, input .TexCoord0 );
115+ const float4 emissiveMap = emissiveTexture .Sample (pbrSampler, input .TexCoord0 );
116+
117+ surface .roughness = clamp(material .metallicRoughnessFactor .y * metallicRoughnessMap .g , 0 . 01 f , 0 . 99 f );
118+ surface .metalness = material .metallicRoughnessFactor .x * metallicRoughnessMap .b ;
119+
120+ surface .diffuseColor = color * (1 . 0 f - surface .metalness );
121+ surface .specularColor = color * surface .metalness ;
122+
123+ // Constant normal incidence Fresnel factor for all dielectrics.
124+ static const float3 dielectricSpec = 0 . 04 f ;
125+ surface .F0 = lerp(dielectricSpec, color .rgb , surface .metalness );
126+
127+ // reflectance equation
128+ float3 Lo = pbrSurfaceColorIbl(surface);
129+
130+ #if defined (USE_SHADOWS )
131+ // Directional light
132+ [branch]
133+ if (lights .directionalLight .intensity > 0 )
134+ {
135+ Lo += pbrDirectionalLight(lights .directionalLight , surface);
136+ }
137+
138+ // Point lights
139+ for (uint i = 0 ; i < lights .pointLightCount ; i++ )
140+ {
141+ PointLight light = pointLights [i];
142+ // calculate per-light radiance and add to outgoing radiance Lo
143+ Lo += pbrPointLight(light, surface);
144+ }
145+
146+ Lo += (surface .diffuseColor * surface .ao * lights .ambient ) + surface .emissive ;
147+ #else
148+ Lo += (surface .diffuseColor * surface .ao ) + surface .emissive ;
149+ #endif
150+
151+ return float4(Lo, surface .alpha );
64152}
0 commit comments