Skip to content

Commit 63d5259

Browse files
authored
Merge pull request #2191 from yaRnMcDonuts/patch-3
Modularize PBRLighting.frag
2 parents 3e3e2b6 + 92d4563 commit 63d5259

File tree

14 files changed

+1339
-2
lines changed

14 files changed

+1339
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#import "Common/ShaderLib/GLSLCompat.glsllib"
2+
3+
// enable apis and import PBRLightingUtils
4+
#define ENABLE_PBRLightingUtils_getWorldPosition 1
5+
#define ENABLE_PBRLightingUtils_getWorldNormal 1
6+
#define ENABLE_PBRLightingUtils_getWorldTangent 1
7+
#define ENABLE_PBRLightingUtils_getTexCoord 1
8+
#define ENABLE_PBRLightingUtils_readPBRSurface 1
9+
#define ENABLE_PBRLightingUtils_computeDirectLightContribution 1
10+
#define ENABLE_PBRLightingUtils_computeProbesContribution 1
11+
12+
#import "Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib"
13+
14+
#ifdef DEBUG_VALUES_MODE
15+
uniform int m_DebugValuesMode;
16+
#endif
17+
18+
uniform vec4 g_LightData[NB_LIGHTS];
19+
uniform vec3 g_CameraPosition;
20+
21+
void main(){
22+
vec3 wpos = PBRLightingUtils_getWorldPosition();
23+
vec3 worldViewDir = normalize(g_CameraPosition - wpos);
24+
25+
// Load surface data
26+
PBRSurface surface=PBRLightingUtils_readPBRSurface(worldViewDir);
27+
28+
// Calculate direct lights
29+
for(int i = 0;i < NB_LIGHTS; i+=3){
30+
vec4 lightData0 = g_LightData[i];
31+
vec4 lightData1 = g_LightData[i+1];
32+
vec4 lightData2 = g_LightData[i+2];
33+
PBRLightingUtils_computeDirectLightContribution(
34+
lightData0, lightData1, lightData2,
35+
surface
36+
);
37+
}
38+
39+
40+
// Calculate env probes
41+
PBRLightingUtils_computeProbesContribution(surface);
42+
43+
// Put it all together
44+
gl_FragColor.rgb = vec3(0.0);
45+
gl_FragColor.rgb += surface.bakedLightContribution;
46+
gl_FragColor.rgb += surface.directLightContribution;
47+
gl_FragColor.rgb += surface.envLightContribution;
48+
gl_FragColor.rgb += surface.emission;
49+
gl_FragColor.a = surface.alpha;
50+
51+
52+
// outputs the final value of the selected layer as a color for debug purposes.
53+
#ifdef DEBUG_VALUES_MODE
54+
if(m_DebugValuesMode == 0){
55+
gl_FragColor.rgb = vec3(surface.albedo);
56+
}
57+
else if(m_DebugValuesMode == 1){
58+
gl_FragColor.rgb = vec3(surface.normal);
59+
}
60+
else if(m_DebugValuesMode == 2){
61+
gl_FragColor.rgb = vec3(surface.roughness);
62+
}
63+
else if(m_DebugValuesMode == 3){
64+
gl_FragColor.rgb = vec3(surface.metallic);
65+
}
66+
else if(m_DebugValuesMode == 4){
67+
gl_FragColor.rgb = surface.ao.rgb;
68+
}
69+
else if(m_DebugValuesMode == 5){
70+
gl_FragColor.rgb = vec3(surface.emission.rgb);
71+
}
72+
#endif
73+
}
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
MaterialDef PBR Lighting {
2+
3+
MaterialParameters {
4+
Int BoundDrawBuffer
5+
6+
// Alpha threshold for fragment discarding
7+
Float AlphaDiscardThreshold (AlphaTestFallOff)
8+
9+
//metallicity of the material
10+
Float Metallic : 1.0
11+
//Roughness of the material
12+
Float Roughness : 1.0
13+
// Base material color
14+
Color BaseColor : 1.0 1.0 1.0 1.0
15+
// The emissive color of the object
16+
Color Emissive
17+
// the emissive power
18+
Float EmissivePower : 3.0
19+
// the emissive intensity
20+
Float EmissiveIntensity : 2.0
21+
22+
// BaseColor map
23+
Texture2D BaseColorMap
24+
25+
// Metallic map
26+
Texture2D MetallicMap -LINEAR
27+
28+
// Roughness Map
29+
Texture2D RoughnessMap -LINEAR
30+
31+
//Metallic and Roughness are packed respectively in the b and g channel of a single map
32+
// r: AO (if AoPackedInMRMap is true)
33+
// g: Roughness
34+
// b: Metallic
35+
Texture2D MetallicRoughnessMap -LINEAR
36+
37+
// Texture of the emissive parts of the material
38+
Texture2D EmissiveMap
39+
40+
// Normal map
41+
Texture2D NormalMap -LINEAR
42+
// The scalar parameter applied to each normal vector of the normal map
43+
Float NormalScale
44+
45+
//The type of normal map: -1.0 (DirectX), 1.0 (OpenGl)
46+
Float NormalType : -1.0
47+
48+
// For Spec gloss pipeline
49+
Boolean UseSpecGloss
50+
Texture2D SpecularMap
51+
Texture2D GlossinessMap
52+
Texture2D SpecularGlossinessMap
53+
Color Specular : 1.0 1.0 1.0 1.0
54+
Float Glossiness : 1.0
55+
56+
// Parallax/height map
57+
Texture2D ParallaxMap -LINEAR
58+
59+
// Specular-AA
60+
Boolean UseSpecularAA : true
61+
// screen space variance,Use the slider to set the strength of the geometric specular anti-aliasing effect between 0 and 1. Higher values produce a blurrier result with less aliasing.
62+
Float SpecularAASigma
63+
// clamping threshold,Use the slider to set a maximum value for the offset that HDRP subtracts from the smoothness value to reduce artifacts.
64+
Float SpecularAAKappa
65+
66+
//Set to true if parallax map is stored in the alpha channel of the normal map
67+
Boolean PackedNormalParallax
68+
69+
//Sets the relief height for parallax mapping
70+
Float ParallaxHeight : 0.05
71+
72+
//Set to true to activate Steep Parallax mapping
73+
Boolean SteepParallax
74+
75+
//Horizon fade
76+
Boolean HorizonFade
77+
78+
// Set to Use Lightmap
79+
Texture2D LightMap
80+
81+
// A scalar multiplier controlling the amount of occlusion applied.
82+
// A value of `0.0` means no occlusion. A value of `1.0` means full occlusion.
83+
Float AoStrength
84+
85+
// Set to use TexCoord2 for the lightmap sampling
86+
Boolean SeparateTexCoord
87+
// the light map is a grayscale ao map, only the r channel will be read.
88+
Boolean LightMapAsAOMap
89+
Boolean AoPackedInMRMap
90+
//shadows
91+
Int FilterMode
92+
Boolean HardwareShadows
93+
94+
Texture2D ShadowMap0
95+
Texture2D ShadowMap1
96+
Texture2D ShadowMap2
97+
Texture2D ShadowMap3
98+
//pointLights
99+
Texture2D ShadowMap4
100+
Texture2D ShadowMap5
101+
102+
Float ShadowIntensity
103+
Vector4 Splits
104+
Vector2 FadeInfo
105+
106+
Matrix4 LightViewProjectionMatrix0
107+
Matrix4 LightViewProjectionMatrix1
108+
Matrix4 LightViewProjectionMatrix2
109+
Matrix4 LightViewProjectionMatrix3
110+
//pointLight
111+
Matrix4 LightViewProjectionMatrix4
112+
Matrix4 LightViewProjectionMatrix5
113+
Vector3 LightPos
114+
Vector3 LightDir
115+
116+
Float PCFEdge
117+
Float ShadowMapSize
118+
119+
// For hardware skinning
120+
Int NumberOfBones
121+
Matrix4Array BoneMatrices
122+
123+
// For Morph animation
124+
FloatArray MorphWeights
125+
Int NumberOfMorphTargets
126+
Int NumberOfTargetsBuffers
127+
128+
// For instancing
129+
Boolean UseInstancing
130+
131+
// For Vertex Color
132+
Boolean UseVertexColor
133+
134+
Boolean BackfaceShadows : false
135+
136+
Boolean UseVertexColorsAsSunIntensity
137+
Float StaticSunIntensity
138+
Boolean BrightenIndoorShadows
139+
140+
Int DebugValuesMode
141+
// debugs the final value of the selected layer as a color output:
142+
// Layers:
143+
// 0 - albedo (unshaded)
144+
// 1 - normals
145+
// 2 - roughness
146+
// 3 - metallic
147+
// 4 - ao
148+
// 5 - emissive
149+
}
150+
151+
Technique {
152+
LightMode SinglePassAndImageBased
153+
154+
VertexShader GLSL300 GLSL150 GLSL110: Common/MatDefs/Light/modular/PBRLighting.vert
155+
FragmentShader GLSL300 GLSL150 GLSL110: Common/MatDefs/Light/modular/PBRLighting.frag
156+
157+
WorldParameters {
158+
WorldViewProjectionMatrix
159+
CameraPosition
160+
WorldMatrix
161+
WorldNormalMatrix
162+
ViewProjectionMatrix
163+
ViewMatrix
164+
}
165+
166+
Defines {
167+
BOUND_DRAW_BUFFER: BoundDrawBuffer
168+
BASECOLORMAP : BaseColorMap
169+
NORMALMAP : NormalMap
170+
NORMALSCALE : NormalScale
171+
METALLICMAP : MetallicMap
172+
ROUGHNESSMAP : RoughnessMap
173+
EMISSIVEMAP : EmissiveMap
174+
EMISSIVE : Emissive
175+
SPECGLOSSPIPELINE : UseSpecGloss
176+
PARALLAXMAP : ParallaxMap
177+
NORMALMAP_PARALLAX : PackedNormalParallax
178+
STEEP_PARALLAX : SteepParallax
179+
LIGHTMAP : LightMap
180+
SEPARATE_TEXCOORD : SeparateTexCoord
181+
DISCARD_ALPHA : AlphaDiscardThreshold
182+
NUM_BONES : NumberOfBones
183+
INSTANCING : UseInstancing
184+
USE_PACKED_MR: MetallicRoughnessMap
185+
USE_PACKED_SG: SpecularGlossinessMap
186+
SPECULARMAP : SpecularMap
187+
SPECULAR_AA : UseSpecularAA
188+
SPECULAR_AA_SCREEN_SPACE_VARIANCE : SpecularAASigma
189+
SPECULAR_AA_THRESHOLD : SpecularAAKappa
190+
GLOSSINESSMAP : GlossinessMap
191+
NORMAL_TYPE: NormalType
192+
VERTEX_COLOR : UseVertexColor
193+
AO_MAP: LightMapAsAOMap
194+
AO_PACKED_IN_MR_MAP : AoPackedInMRMap
195+
AO_STRENGTH : AoStrength
196+
NUM_MORPH_TARGETS: NumberOfMorphTargets
197+
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
198+
HORIZON_FADE: HorizonFade
199+
USE_VERTEX_COLORS_AS_SUN_INTENSITY : UseVertexColorsAsSunIntensity
200+
STATIC_SUN_INTENSITY : StaticSunIntensity
201+
BRIGHTEN_INDOOR_SHADOWS : BrightenIndoorShadows
202+
DEBUG_VALUES_MODE : DebugValuesMode
203+
}
204+
}
205+
206+
207+
Technique PreShadow {
208+
209+
VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PreShadow.vert
210+
FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PreShadowPBR.frag
211+
212+
WorldParameters {
213+
WorldViewProjectionMatrix
214+
WorldViewMatrix
215+
ViewProjectionMatrix
216+
ViewMatrix
217+
}
218+
219+
Defines {
220+
BOUND_DRAW_BUFFER: BoundDrawBuffer
221+
DISCARD_ALPHA : AlphaDiscardThreshold
222+
NUM_BONES : NumberOfBones
223+
INSTANCING : UseInstancing
224+
NUM_MORPH_TARGETS: NumberOfMorphTargets
225+
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
226+
}
227+
228+
ForcedRenderState {
229+
FaceCull Off
230+
DepthTest On
231+
DepthWrite On
232+
PolyOffset 5 3
233+
ColorWrite Off
234+
}
235+
236+
}
237+
238+
239+
Technique PostShadow {
240+
VertexShader GLSL310 GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PostShadow.vert
241+
FragmentShader GLSL310 GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PostShadowPBR.frag
242+
243+
WorldParameters {
244+
WorldViewProjectionMatrix
245+
WorldMatrix
246+
ViewProjectionMatrix
247+
ViewMatrix
248+
}
249+
250+
Defines {
251+
BOUND_DRAW_BUFFER: BoundDrawBuffer
252+
HARDWARE_SHADOWS : HardwareShadows
253+
FILTER_MODE : FilterMode
254+
PCFEDGE : PCFEdge
255+
DISCARD_ALPHA : AlphaDiscardThreshold
256+
SHADOWMAP_SIZE : ShadowMapSize
257+
FADE : FadeInfo
258+
PSSM : Splits
259+
POINTLIGHT : LightViewProjectionMatrix5
260+
NUM_BONES : NumberOfBones
261+
INSTANCING : UseInstancing
262+
BACKFACE_SHADOWS: BackfaceShadows
263+
NUM_MORPH_TARGETS: NumberOfMorphTargets
264+
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
265+
}
266+
267+
ForcedRenderState {
268+
Blend Modulate
269+
DepthWrite Off
270+
PolyOffset -0.1 0
271+
}
272+
}
273+
274+
Technique PreNormalPass {
275+
276+
VertexShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/SSAO/normal.vert
277+
FragmentShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/SSAO/normal.frag
278+
279+
WorldParameters {
280+
WorldViewProjectionMatrix
281+
WorldViewMatrix
282+
NormalMatrix
283+
ViewProjectionMatrix
284+
ViewMatrix
285+
}
286+
287+
Defines {
288+
BOUND_DRAW_BUFFER: BoundDrawBuffer
289+
BASECOLORMAP_ALPHA : BaseColorMap
290+
NUM_BONES : NumberOfBones
291+
INSTANCING : UseInstancing
292+
NUM_MORPH_TARGETS: NumberOfMorphTargets
293+
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
294+
}
295+
296+
}
297+
298+
Technique Glow {
299+
300+
VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Misc/Unshaded.vert
301+
FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Light/PBRGlow.frag
302+
303+
WorldParameters {
304+
WorldViewProjectionMatrix
305+
ViewProjectionMatrix
306+
ViewMatrix
307+
}
308+
309+
Defines {
310+
HAS_EMISSIVEMAP : EmissiveMap
311+
HAS_EMISSIVECOLOR : Emissive
312+
BOUND_DRAW_BUFFER: BoundDrawBuffer
313+
NEED_TEXCOORD1
314+
NUM_BONES : NumberOfBones
315+
INSTANCING : UseInstancing
316+
NUM_MORPH_TARGETS: NumberOfMorphTargets
317+
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
318+
}
319+
}
320+
321+
}

0 commit comments

Comments
 (0)