git.s-ol.nu ~forks/DiligentFX / 6155b21
GLTF PBR Renderer: fixed shader to support cached models that don't have certain texture maps (AO, emissive, etc.) assiduous 10 months ago
2 changed file(s) with 60 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
5959 SamplerState g_EmissiveMap_sampler;
6060 #endif
6161
62 float2 GetTextureUV(in float2 UV0, in float2 UV1, float Selector, float4 ScaleBias)
62 float4 SampleGLTFTexture(Texture2DArray Tex,
63 SamplerState Tex_sampler,
64 float2 UV0,
65 float2 UV1,
66 float Selector,
67 float4 ScaleBias,
68 float Slice,
69 float4 DefaultValue)
6370 {
6471 float2 UV = lerp(UV0, UV1, Selector);
6572 #if USE_TEXTURE_ATLAS
73 if (Selector < 0.0)
74 {
75 return DefaultValue;
76 }
77 else
6678 {
6779 UV = frac(UV);
6880 UV = UV * ScaleBias.xy + ScaleBias.zw;
6981 }
7082 #endif
71 return UV;
72 }
73
83 return Tex.Sample(Tex_sampler, float3(UV, Slice));
84 }
7485
7586 void main(in float4 ClipPos : SV_Position,
7687 in float3 WorldPos : WORLD_POS,
8091 in bool IsFrontFace : SV_IsFrontFace,
8192 out float4 OutColor : SV_Target)
8293 {
83 float2 BaseColorUV = GetTextureUV(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector, g_MaterialInfo.BaseColorUVScaleBias);
84 float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, float3(BaseColorUV, g_MaterialInfo.BaseColorSlice));
94 float4 BaseColor = SampleGLTFTexture(g_ColorMap, g_ColorMap_sampler, UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector,
95 g_MaterialInfo.BaseColorUVScaleBias, g_MaterialInfo.BaseColorSlice, float4(1.0, 1.0, 1.0, 1.0));
8596 BaseColor = SRGBtoLINEAR(BaseColor) * g_MaterialInfo.BaseColorFactor;
8697 //BaseColor *= getVertexColor();
8798
106117 discard;
107118 }
108119
109 float3 TSNormal = g_NormalMap.Sample(g_NormalMap_sampler, float3(NormalMapUV, g_MaterialInfo.NormalSlice)).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);
120 float3 TSNormal = g_MaterialInfo.NormalTextureUVSelector >= 0.0 ?
121 g_NormalMap.Sample(g_NormalMap_sampler, float3(NormalMapUV, g_MaterialInfo.NormalSlice)).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0) :
122 float3(0.0, 0.0, 1.0);
110123
111124 float Occlusion = 1.0;
112125 #if GLTF_PBR_USE_AO
113 {
114 float2 OcclusionUV = GetTextureUV(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector, g_MaterialInfo.OcclusionUVScaleBias);
115 Occlusion = g_AOMap.Sample(g_AOMap_sampler, float3(OcclusionUV, g_MaterialInfo.OcclusionSlice)).r;
116 }
126 Occlusion = SampleGLTFTexture(g_AOMap, g_AOMap_sampler, UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector,
127 g_MaterialInfo.OcclusionUVScaleBias, g_MaterialInfo.OcclusionSlice, float4(1.0, 1.0, 1.0, 1.0)).r;
117128 #endif
118129
119130 float3 Emissive = float3(0.0, 0.0, 0.0);
120131 #if GLTF_PBR_USE_EMISSIVE
121 {
122 float2 EmissiveUV = GetTextureUV(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector, g_MaterialInfo.EmissiveUVScaleBias);
123 Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, float3(EmissiveUV, g_MaterialInfo.EmissiveSlice)).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, float3(PhysicalDescUV, g_MaterialInfo.PhysicalDescriptorSlice));
132 Emissive = SampleGLTFTexture(g_EmissiveMap, g_EmissiveMap_sampler, UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector,
133 g_MaterialInfo.EmissiveUVScaleBias, g_MaterialInfo.EmissiveSlice, float4(0.0, 0.0, 0.0, 0.0)).rgb;
134 #endif
135
136 float4 PhysicalDesc = SampleGLTFTexture(g_PhysicalDescriptorMap, g_PhysicalDescriptorMap_sampler,
137 UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector,
138 g_MaterialInfo.PhysicalDescriptorUVScaleBias, g_MaterialInfo.PhysicalDescriptorSlice,
139 float4(0.0, 1.0, 0.0, 0.0));
130140
131141 float metallic;
132142 if (g_MaterialInfo.Workflow == PBR_WORKFLOW_SPECULAR_GLOSINESS)
5959 "SamplerState g_EmissiveMap_sampler;\n"
6060 "#endif\n"
6161 "\n"
62 "float2 GetTextureUV(in float2 UV0, in float2 UV1, float Selector, float4 ScaleBias)\n"
62 "float4 SampleGLTFTexture(Texture2DArray Tex,\n"
63 " SamplerState Tex_sampler,\n"
64 " float2 UV0,\n"
65 " float2 UV1,\n"
66 " float Selector, \n"
67 " float4 ScaleBias,\n"
68 " float Slice,\n"
69 " float4 DefaultValue)\n"
6370 "{\n"
6471 " float2 UV = lerp(UV0, UV1, Selector);\n"
6572 "#if USE_TEXTURE_ATLAS\n"
73 " if (Selector < 0.0)\n"
74 " {\n"
75 " return DefaultValue;\n"
76 " }\n"
77 " else\n"
6678 " {\n"
6779 " UV = frac(UV);\n"
6880 " UV = UV * ScaleBias.xy + ScaleBias.zw;\n"
6981 " }\n"
7082 "#endif\n"
71 " return UV;\n"
72 "}\n"
73 "\n"
83 " return Tex.Sample(Tex_sampler, float3(UV, Slice));\n"
84 "}\n"
7485 "\n"
7586 "void main(in float4 ClipPos : SV_Position,\n"
7687 " in float3 WorldPos : WORLD_POS,\n"
8091 " in bool IsFrontFace : SV_IsFrontFace,\n"
8192 " out float4 OutColor : SV_Target)\n"
8293 "{\n"
83 " float2 BaseColorUV = GetTextureUV(UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector, g_MaterialInfo.BaseColorUVScaleBias);\n"
84 " float4 BaseColor = g_ColorMap.Sample(g_ColorMap_sampler, float3(BaseColorUV, g_MaterialInfo.BaseColorSlice));\n"
94 " float4 BaseColor = SampleGLTFTexture(g_ColorMap, g_ColorMap_sampler, UV0, UV1, g_MaterialInfo.BaseColorTextureUVSelector,\n"
95 " g_MaterialInfo.BaseColorUVScaleBias, g_MaterialInfo.BaseColorSlice, float4(1.0, 1.0, 1.0, 1.0));\n"
8596 " BaseColor = SRGBtoLINEAR(BaseColor) * g_MaterialInfo.BaseColorFactor;\n"
8697 " //BaseColor *= getVertexColor();\n"
8798 "\n"
106117 " discard;\n"
107118 " }\n"
108119 "\n"
109 " float3 TSNormal = g_NormalMap.Sample(g_NormalMap_sampler, float3(NormalMapUV, g_MaterialInfo.NormalSlice)).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);\n"
120 " float3 TSNormal = g_MaterialInfo.NormalTextureUVSelector >= 0.0 ?\n"
121 " g_NormalMap.Sample(g_NormalMap_sampler, float3(NormalMapUV, g_MaterialInfo.NormalSlice)).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0) : \n"
122 " float3(0.0, 0.0, 1.0);\n"
110123 "\n"
111124 " float Occlusion = 1.0;\n"
112125 "#if GLTF_PBR_USE_AO\n"
113 " {\n"
114 " float2 OcclusionUV = GetTextureUV(UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector, g_MaterialInfo.OcclusionUVScaleBias);\n"
115 " Occlusion = g_AOMap.Sample(g_AOMap_sampler, float3(OcclusionUV, g_MaterialInfo.OcclusionSlice)).r;\n"
116 " }\n"
126 " Occlusion = SampleGLTFTexture(g_AOMap, g_AOMap_sampler, UV0, UV1, g_MaterialInfo.OcclusionTextureUVSelector, \n"
127 " g_MaterialInfo.OcclusionUVScaleBias, g_MaterialInfo.OcclusionSlice, float4(1.0, 1.0, 1.0, 1.0)).r;\n"
117128 "#endif\n"
118129 "\n"
119130 " float3 Emissive = float3(0.0, 0.0, 0.0);\n"
120131 "#if GLTF_PBR_USE_EMISSIVE\n"
121 " {\n"
122 " float2 EmissiveUV = GetTextureUV(UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector, g_MaterialInfo.EmissiveUVScaleBias);\n"
123 " Emissive = g_EmissiveMap.Sample(g_EmissiveMap_sampler, float3(EmissiveUV, g_MaterialInfo.EmissiveSlice)).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, float3(PhysicalDescUV, g_MaterialInfo.PhysicalDescriptorSlice));\n"
132 " Emissive = SampleGLTFTexture(g_EmissiveMap, g_EmissiveMap_sampler, UV0, UV1, g_MaterialInfo.EmissiveTextureUVSelector, \n"
133 " g_MaterialInfo.EmissiveUVScaleBias, g_MaterialInfo.EmissiveSlice, float4(0.0, 0.0, 0.0, 0.0)).rgb;\n"
134 "#endif\n"
135 "\n"
136 " float4 PhysicalDesc = SampleGLTFTexture(g_PhysicalDescriptorMap, g_PhysicalDescriptorMap_sampler, \n"
137 " UV0, UV1, g_MaterialInfo.PhysicalDescriptorTextureUVSelector, \n"
138 " g_MaterialInfo.PhysicalDescriptorUVScaleBias, g_MaterialInfo.PhysicalDescriptorSlice,\n"
139 " float4(0.0, 1.0, 0.0, 0.0));\n"
130140 " \n"
131141 " float metallic;\n"
132142 " if (g_MaterialInfo.Workflow == PBR_WORKFLOW_SPECULAR_GLOSINESS)\n"