Skip to content

Commit

Permalink
Merge pull request #2191 from yaRnMcDonuts/patch-3
Browse files Browse the repository at this point in the history
Modularize PBRLighting.frag
  • Loading branch information
yaRnMcDonuts authored Jan 5, 2025
2 parents 3e3e2b6 + 92d4563 commit 63d5259
Show file tree
Hide file tree
Showing 14 changed files with 1,339 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#import "Common/ShaderLib/GLSLCompat.glsllib"

// enable apis and import PBRLightingUtils
#define ENABLE_PBRLightingUtils_getWorldPosition 1
#define ENABLE_PBRLightingUtils_getWorldNormal 1
#define ENABLE_PBRLightingUtils_getWorldTangent 1
#define ENABLE_PBRLightingUtils_getTexCoord 1
#define ENABLE_PBRLightingUtils_readPBRSurface 1
#define ENABLE_PBRLightingUtils_computeDirectLightContribution 1
#define ENABLE_PBRLightingUtils_computeProbesContribution 1

#import "Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib"

#ifdef DEBUG_VALUES_MODE
uniform int m_DebugValuesMode;
#endif

uniform vec4 g_LightData[NB_LIGHTS];
uniform vec3 g_CameraPosition;

void main(){
vec3 wpos = PBRLightingUtils_getWorldPosition();
vec3 worldViewDir = normalize(g_CameraPosition - wpos);

// Load surface data
PBRSurface surface=PBRLightingUtils_readPBRSurface(worldViewDir);

// Calculate direct lights
for(int i = 0;i < NB_LIGHTS; i+=3){
vec4 lightData0 = g_LightData[i];
vec4 lightData1 = g_LightData[i+1];
vec4 lightData2 = g_LightData[i+2];
PBRLightingUtils_computeDirectLightContribution(
lightData0, lightData1, lightData2,
surface
);
}


// Calculate env probes
PBRLightingUtils_computeProbesContribution(surface);

// Put it all together
gl_FragColor.rgb = vec3(0.0);
gl_FragColor.rgb += surface.bakedLightContribution;
gl_FragColor.rgb += surface.directLightContribution;
gl_FragColor.rgb += surface.envLightContribution;
gl_FragColor.rgb += surface.emission;
gl_FragColor.a = surface.alpha;


// outputs the final value of the selected layer as a color for debug purposes.
#ifdef DEBUG_VALUES_MODE
if(m_DebugValuesMode == 0){
gl_FragColor.rgb = vec3(surface.albedo);
}
else if(m_DebugValuesMode == 1){
gl_FragColor.rgb = vec3(surface.normal);
}
else if(m_DebugValuesMode == 2){
gl_FragColor.rgb = vec3(surface.roughness);
}
else if(m_DebugValuesMode == 3){
gl_FragColor.rgb = vec3(surface.metallic);
}
else if(m_DebugValuesMode == 4){
gl_FragColor.rgb = surface.ao.rgb;
}
else if(m_DebugValuesMode == 5){
gl_FragColor.rgb = vec3(surface.emission.rgb);
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
MaterialDef PBR Lighting {

MaterialParameters {
Int BoundDrawBuffer

// Alpha threshold for fragment discarding
Float AlphaDiscardThreshold (AlphaTestFallOff)

//metallicity of the material
Float Metallic : 1.0
//Roughness of the material
Float Roughness : 1.0
// Base material color
Color BaseColor : 1.0 1.0 1.0 1.0
// The emissive color of the object
Color Emissive
// the emissive power
Float EmissivePower : 3.0
// the emissive intensity
Float EmissiveIntensity : 2.0

// BaseColor map
Texture2D BaseColorMap

// Metallic map
Texture2D MetallicMap -LINEAR

// Roughness Map
Texture2D RoughnessMap -LINEAR

//Metallic and Roughness are packed respectively in the b and g channel of a single map
// r: AO (if AoPackedInMRMap is true)
// g: Roughness
// b: Metallic
Texture2D MetallicRoughnessMap -LINEAR

// Texture of the emissive parts of the material
Texture2D EmissiveMap

// Normal map
Texture2D NormalMap -LINEAR
// The scalar parameter applied to each normal vector of the normal map
Float NormalScale

//The type of normal map: -1.0 (DirectX), 1.0 (OpenGl)
Float NormalType : -1.0

// For Spec gloss pipeline
Boolean UseSpecGloss
Texture2D SpecularMap
Texture2D GlossinessMap
Texture2D SpecularGlossinessMap
Color Specular : 1.0 1.0 1.0 1.0
Float Glossiness : 1.0

// Parallax/height map
Texture2D ParallaxMap -LINEAR

// Specular-AA
Boolean UseSpecularAA : true
// 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.
Float SpecularAASigma
// clamping threshold,Use the slider to set a maximum value for the offset that HDRP subtracts from the smoothness value to reduce artifacts.
Float SpecularAAKappa

//Set to true if parallax map is stored in the alpha channel of the normal map
Boolean PackedNormalParallax

//Sets the relief height for parallax mapping
Float ParallaxHeight : 0.05

//Set to true to activate Steep Parallax mapping
Boolean SteepParallax

//Horizon fade
Boolean HorizonFade

// Set to Use Lightmap
Texture2D LightMap

// A scalar multiplier controlling the amount of occlusion applied.
// A value of `0.0` means no occlusion. A value of `1.0` means full occlusion.
Float AoStrength

// Set to use TexCoord2 for the lightmap sampling
Boolean SeparateTexCoord
// the light map is a grayscale ao map, only the r channel will be read.
Boolean LightMapAsAOMap
Boolean AoPackedInMRMap
//shadows
Int FilterMode
Boolean HardwareShadows

Texture2D ShadowMap0
Texture2D ShadowMap1
Texture2D ShadowMap2
Texture2D ShadowMap3
//pointLights
Texture2D ShadowMap4
Texture2D ShadowMap5

Float ShadowIntensity
Vector4 Splits
Vector2 FadeInfo

Matrix4 LightViewProjectionMatrix0
Matrix4 LightViewProjectionMatrix1
Matrix4 LightViewProjectionMatrix2
Matrix4 LightViewProjectionMatrix3
//pointLight
Matrix4 LightViewProjectionMatrix4
Matrix4 LightViewProjectionMatrix5
Vector3 LightPos
Vector3 LightDir

Float PCFEdge
Float ShadowMapSize

// For hardware skinning
Int NumberOfBones
Matrix4Array BoneMatrices

// For Morph animation
FloatArray MorphWeights
Int NumberOfMorphTargets
Int NumberOfTargetsBuffers

// For instancing
Boolean UseInstancing

// For Vertex Color
Boolean UseVertexColor

Boolean BackfaceShadows : false

Boolean UseVertexColorsAsSunIntensity
Float StaticSunIntensity
Boolean BrightenIndoorShadows

Int DebugValuesMode
// debugs the final value of the selected layer as a color output:
// Layers:
// 0 - albedo (unshaded)
// 1 - normals
// 2 - roughness
// 3 - metallic
// 4 - ao
// 5 - emissive
}

Technique {
LightMode SinglePassAndImageBased

VertexShader GLSL300 GLSL150 GLSL110: Common/MatDefs/Light/modular/PBRLighting.vert
FragmentShader GLSL300 GLSL150 GLSL110: Common/MatDefs/Light/modular/PBRLighting.frag

WorldParameters {
WorldViewProjectionMatrix
CameraPosition
WorldMatrix
WorldNormalMatrix
ViewProjectionMatrix
ViewMatrix
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
BASECOLORMAP : BaseColorMap
NORMALMAP : NormalMap
NORMALSCALE : NormalScale
METALLICMAP : MetallicMap
ROUGHNESSMAP : RoughnessMap
EMISSIVEMAP : EmissiveMap
EMISSIVE : Emissive
SPECGLOSSPIPELINE : UseSpecGloss
PARALLAXMAP : ParallaxMap
NORMALMAP_PARALLAX : PackedNormalParallax
STEEP_PARALLAX : SteepParallax
LIGHTMAP : LightMap
SEPARATE_TEXCOORD : SeparateTexCoord
DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
USE_PACKED_MR: MetallicRoughnessMap
USE_PACKED_SG: SpecularGlossinessMap
SPECULARMAP : SpecularMap
SPECULAR_AA : UseSpecularAA
SPECULAR_AA_SCREEN_SPACE_VARIANCE : SpecularAASigma
SPECULAR_AA_THRESHOLD : SpecularAAKappa
GLOSSINESSMAP : GlossinessMap
NORMAL_TYPE: NormalType
VERTEX_COLOR : UseVertexColor
AO_MAP: LightMapAsAOMap
AO_PACKED_IN_MR_MAP : AoPackedInMRMap
AO_STRENGTH : AoStrength
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
HORIZON_FADE: HorizonFade
USE_VERTEX_COLORS_AS_SUN_INTENSITY : UseVertexColorsAsSunIntensity
STATIC_SUN_INTENSITY : StaticSunIntensity
BRIGHTEN_INDOOR_SHADOWS : BrightenIndoorShadows
DEBUG_VALUES_MODE : DebugValuesMode
}
}


Technique PreShadow {

VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PreShadow.vert
FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PreShadowPBR.frag

WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
ViewProjectionMatrix
ViewMatrix
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
}

ForcedRenderState {
FaceCull Off
DepthTest On
DepthWrite On
PolyOffset 5 3
ColorWrite Off
}

}


Technique PostShadow {
VertexShader GLSL310 GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PostShadow.vert
FragmentShader GLSL310 GLSL300 GLSL150 GLSL100: Common/MatDefs/Shadow/PostShadowPBR.frag

WorldParameters {
WorldViewProjectionMatrix
WorldMatrix
ViewProjectionMatrix
ViewMatrix
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
HARDWARE_SHADOWS : HardwareShadows
FILTER_MODE : FilterMode
PCFEDGE : PCFEdge
DISCARD_ALPHA : AlphaDiscardThreshold
SHADOWMAP_SIZE : ShadowMapSize
FADE : FadeInfo
PSSM : Splits
POINTLIGHT : LightViewProjectionMatrix5
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
BACKFACE_SHADOWS: BackfaceShadows
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
}

ForcedRenderState {
Blend Modulate
DepthWrite Off
PolyOffset -0.1 0
}
}

Technique PreNormalPass {

VertexShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/SSAO/normal.vert
FragmentShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/SSAO/normal.frag

WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
NormalMatrix
ViewProjectionMatrix
ViewMatrix
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
BASECOLORMAP_ALPHA : BaseColorMap
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
}

}

Technique Glow {

VertexShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Misc/Unshaded.vert
FragmentShader GLSL300 GLSL150 GLSL100: Common/MatDefs/Light/PBRGlow.frag

WorldParameters {
WorldViewProjectionMatrix
ViewProjectionMatrix
ViewMatrix
}

Defines {
HAS_EMISSIVEMAP : EmissiveMap
HAS_EMISSIVECOLOR : Emissive
BOUND_DRAW_BUFFER: BoundDrawBuffer
NEED_TEXCOORD1
NUM_BONES : NumberOfBones
INSTANCING : UseInstancing
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
}
}

}
Loading

0 comments on commit 63d5259

Please sign in to comment.