git.s-ol.nu ~forks/DiligentFX / 45e39b6
GLTF PBR Renderer: added option to use texture atlas assiduous 10 months ago
6 changed file(s) with 105 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
7878 /// When set to false, samplers from the texture views will be used.
7979 bool UseImmutableSamplers = true;
8080
81 /// Whether to use texture atlas (e.g. apply UV transforms when sampling textures).
82 bool UseTextureAtals = false;
83
8184 static const SamplerDesc DefaultSampler;
8285
8386 /// Immutable sampler for color map texture.
244244 Macros.AddShaderMacro("GLTF_PBR_USE_IBL", m_Settings.UseIBL);
245245 Macros.AddShaderMacro("GLTF_PBR_USE_AO", m_Settings.UseAO);
246246 Macros.AddShaderMacro("GLTF_PBR_USE_EMISSIVE", m_Settings.UseEmissive);
247 Macros.AddShaderMacro("USE_TEXTURE_ATLAS", m_Settings.UseTextureAtals);
247248 ShaderCI.Macros = Macros;
248249 RefCntAutoPtr<IShader> pVS;
249250 {
848849 pMaterialInfo->UseAlphaMask = material.AlphaMode == GLTF::Material::ALPHAMODE_MASK ? 1 : 0;
849850 pMaterialInfo->AlphaMaskCutoff = material.AlphaCutoff;
850851
852 pMaterialInfo->BaseColorUVScaleBias = material.TexCoordSets.BaseColorScaleBias;
853 pMaterialInfo->NormalMapUVScaleBias = material.TexCoordSets.NormalScaleBias;
854 pMaterialInfo->OcclusionUVScaleBias = material.TexCoordSets.OcclusionScaleBias;
855 pMaterialInfo->EmissiveUVScaleBias = material.TexCoordSets.EmissiveScaleBias;
856
851857 // TODO: glTF specs states that metallic roughness should be preferred, even if specular glosiness is present
852858 if (material.workflow == GLTF::Material::PbrWorkflow::MetallicRoughness)
853859 {
858864 pMaterialInfo->RoughnessFactor = material.RoughnessFactor;
859865 pMaterialInfo->PhysicalDescriptorTextureUVSelector = GetUVSelector(material.pMetallicRoughnessTexture, material.TexCoordSets.MetallicRoughness);
860866 pMaterialInfo->BaseColorTextureUVSelector = GetUVSelector(material.pBaseColorTexture, material.TexCoordSets.BaseColor);
867 pMaterialInfo->PhysicalDescriptorUVScaleBias = material.TexCoordSets.MetallicRoughnessScaleBias;
861868 }
862869 else if (material.workflow == GLTF::Material::PbrWorkflow::SpecularGlossiness)
863870 {
867874 pMaterialInfo->BaseColorTextureUVSelector = GetUVSelector(material.extension.pDiffuseTexture, material.TexCoordSets.BaseColor);
868875 pMaterialInfo->BaseColorFactor = material.extension.DiffuseFactor;
869876 pMaterialInfo->SpecularFactor = float4(material.extension.SpecularFactor, 1.0f);
877 pMaterialInfo->PhysicalDescriptorUVScaleBias = material.TexCoordSets.SpecularGlossinessScaleBias;
870878 }
871879
872880 if (RenderNodeCallback == nullptr)
99 # define ALLOW_DEBUG_VIEW 0
1010 #endif
1111
12 #ifndef USE_TEXTURE_ATLAS
13 # define USE_TEXTURE_ATLAS 0
14 #endif
15
1216 cbuffer cbCameraAttribs
1317 {
1418 CameraAttribs g_CameraAttribs;
5458 Texture2D g_EmissiveMap;
5559 SamplerState g_EmissiveMap_sampler;
5660 #endif
61
62 float2 GetTextureUV(in float2 UV0, in float2 UV1, float Selector, float4 ScaleBias)
63 {
64 float2 UV = lerp(UV0, UV1, Selector);
65 #if USE_TEXTURE_ATLAS
66 {
67 UV = frac(UV);
68 UV = UV * ScaleBias.xy + ScaleBias.zw;
69 }
70 #endif
71 return UV;
72 }
5773
5874
5975 void main(in float4 ClipPos : SV_Position,
6480 in bool IsFrontFace : SV_IsFrontFace,
6581 out float4 OutColor : SV_Target)
6682 {
67 float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, lerp(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector));
83 float2 BaseColorUV = GetTextureUV(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector, g_MaterialInfo.BaseColorUVScaleBias);
84 float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, BaseColorUV);
6885 BaseColor = SRGBtoLINEAR(BaseColor) * g_MaterialInfo.BaseColorFactor;
6986 //BaseColor *= getVertexColor();
7087
7592 float3 dWorldPos_dy = ddy(WorldPos);
7693 float2 dNormalMapUV_dx = ddx(NormalMapUV);
7794 float2 dNormalMapUV_dy = ddy(NormalMapUV);
95 #if USE_TEXTURE_ATLAS
96 {
97 NormalMapUV = frac(NormalMapUV);
98 NormalMapUV = NormalMapUV * g_MaterialInfo.NormalMapUVScaleBias.xy + g_MaterialInfo.NormalMapUVScaleBias.zw;
99 dNormalMapUV_dx *= g_MaterialInfo.NormalMapUVScaleBias.xy;
100 dNormalMapUV_dy *= g_MaterialInfo.NormalMapUVScaleBias.xy;
101 }
102 #endif
78103
79104 if (g_MaterialInfo.UseAlphaMask != 0 && BaseColor.a < g_MaterialInfo.AlphaMaskCutoff)
80105 {
85110
86111 float Occlusion = 1.0;
87112 #if GLTF_PBR_USE_AO
88 Occlusion = g_AOMap.Sample(g_AOMap_sampler, lerp(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector)).r;
113 {
114 float2 OcclusionUV = GetTextureUV(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector, g_MaterialInfo.OcclusionUVScaleBias);
115 Occlusion = g_AOMap.Sample(g_AOMap_sampler, OcclusionUV).r;
116 }
89117 #endif
90118
91119 float3 Emissive = float3(0.0, 0.0, 0.0);
92120 #if GLTF_PBR_USE_EMISSIVE
93 Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, lerp(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector)).rgb;
94 #endif
95
96 float4 PhysicalDesc = g_PhysicalDescriptorMap.Sample(g_PhysicalDescriptorMap_sampler, lerp(UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector));
121 {
122 float2 EmissiveUV = GetTextureUV(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector, g_MaterialInfo.EmissiveUVScaleBias);
123 Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, EmissiveUV).rgb;
124 }
125 #endif
126
127
128 float2 PhysicalDescUV = GetTextureUV(UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector, g_MaterialInfo.PhysicalDescriptorUVScaleBias);
129 float4 PhysicalDesc = g_PhysicalDescriptorMap.Sample(g_PhysicalDescriptorMap_sampler, PhysicalDescUV);
97130
98131 float metallic;
99132 if (g_MaterialInfo.Workflow == PBR_WORKFLOW_SPECULAR_GLOSINESS)
6565 float MetallicFactor;
6666 float RoughnessFactor;
6767
68 // When texture atlas is used, UV scale and bias applied to
69 // each texture coordinate set
70 float4 BaseColorUVScaleBias;
71 float4 PhysicalDescriptorUVScaleBias;
72 float4 NormalMapUVScaleBias;
73 float4 OcclusionUVScaleBias;
74 float4 EmissiveUVScaleBias;
75
6876 int UseAlphaMask;
6977 float AlphaMaskCutoff;
7078 float Dummy0;
6565 " float MetallicFactor;\n"
6666 " float RoughnessFactor;\n"
6767 "\n"
68 " // When texture atlas is used, UV scale and bias applied to\n"
69 " // each texture coordinate set\n"
70 " float4 BaseColorUVScaleBias;\n"
71 " float4 PhysicalDescriptorUVScaleBias;\n"
72 " float4 NormalMapUVScaleBias;\n"
73 " float4 OcclusionUVScaleBias;\n"
74 " float4 EmissiveUVScaleBias;\n"
75 "\n"
6876 " int UseAlphaMask; \n"
6977 " float AlphaMaskCutoff;\n"
7078 " float Dummy0;\n"
99 "# define ALLOW_DEBUG_VIEW 0\n"
1010 "#endif\n"
1111 "\n"
12 "#ifndef USE_TEXTURE_ATLAS\n"
13 "# define USE_TEXTURE_ATLAS 0\n"
14 "#endif\n"
15 "\n"
1216 "cbuffer cbCameraAttribs\n"
1317 "{\n"
1418 " CameraAttribs g_CameraAttribs;\n"
5458 "Texture2D g_EmissiveMap;\n"
5559 "SamplerState g_EmissiveMap_sampler;\n"
5660 "#endif\n"
61 "\n"
62 "float2 GetTextureUV(in float2 UV0, in float2 UV1, float Selector, float4 ScaleBias)\n"
63 "{\n"
64 " float2 UV = lerp(UV0, UV1, Selector);\n"
65 "#if USE_TEXTURE_ATLAS\n"
66 " {\n"
67 " UV = frac(UV);\n"
68 " UV = UV * ScaleBias.xy + ScaleBias.zw;\n"
69 " }\n"
70 "#endif\n"
71 " return UV;\n"
72 "}\n"
5773 "\n"
5874 "\n"
5975 "void main(in float4 ClipPos : SV_Position,\n"
6480 " in bool IsFrontFace : SV_IsFrontFace,\n"
6581 " out float4 OutColor : SV_Target)\n"
6682 "{\n"
67 " float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, lerp(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector));\n"
83 " float2 BaseColorUV = GetTextureUV(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector, g_MaterialInfo.BaseColorUVScaleBias);\n"
84 " float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, BaseColorUV);\n"
6885 " BaseColor = SRGBtoLINEAR(BaseColor) * g_MaterialInfo.BaseColorFactor;\n"
6986 " //BaseColor *= getVertexColor();\n"
7087 "\n"
7592 " float3 dWorldPos_dy = ddy(WorldPos);\n"
7693 " float2 dNormalMapUV_dx = ddx(NormalMapUV);\n"
7794 " float2 dNormalMapUV_dy = ddy(NormalMapUV);\n"
95 "#if USE_TEXTURE_ATLAS\n"
96 " {\n"
97 " NormalMapUV = frac(NormalMapUV);\n"
98 " NormalMapUV = NormalMapUV * g_MaterialInfo.NormalMapUVScaleBias.xy + g_MaterialInfo.NormalMapUVScaleBias.zw;\n"
99 " dNormalMapUV_dx *= g_MaterialInfo.NormalMapUVScaleBias.xy;\n"
100 " dNormalMapUV_dy *= g_MaterialInfo.NormalMapUVScaleBias.xy;\n"
101 " }\n"
102 "#endif\n"
78103 "\n"
79104 " if (g_MaterialInfo.UseAlphaMask != 0 && BaseColor.a < g_MaterialInfo.AlphaMaskCutoff)\n"
80105 " {\n"
85110 "\n"
86111 " float Occlusion = 1.0;\n"
87112 "#if GLTF_PBR_USE_AO\n"
88 " Occlusion = g_AOMap.Sample(g_AOMap_sampler, lerp(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector)).r;\n"
113 " {\n"
114 " float2 OcclusionUV = GetTextureUV(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector, g_MaterialInfo.OcclusionUVScaleBias);\n"
115 " Occlusion = g_AOMap.Sample(g_AOMap_sampler, OcclusionUV).r;\n"
116 " }\n"
89117 "#endif\n"
90118 "\n"
91119 " float3 Emissive = float3(0.0, 0.0, 0.0);\n"
92120 "#if GLTF_PBR_USE_EMISSIVE\n"
93 " Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, lerp(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector)).rgb;\n"
94 "#endif\n"
95 "\n"
96 " float4 PhysicalDesc = g_PhysicalDescriptorMap.Sample(g_PhysicalDescriptorMap_sampler, lerp(UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector));\n"
121 " {\n"
122 " float2 EmissiveUV = GetTextureUV(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector, g_MaterialInfo.EmissiveUVScaleBias);\n"
123 " Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, EmissiveUV).rgb;\n"
124 " }\n"
125 "#endif\n"
126 "\n"
127 "\n"
128 " float2 PhysicalDescUV = GetTextureUV(UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector, g_MaterialInfo.PhysicalDescriptorUVScaleBias);\n"
129 " float4 PhysicalDesc = g_PhysicalDescriptorMap.Sample(g_PhysicalDescriptorMap_sampler, PhysicalDescUV);\n"
97130 " \n"
98131 " float metallic;\n"
99132 " if (g_MaterialInfo.Workflow == PBR_WORKFLOW_SPECULAR_GLOSINESS)\n"