CG Kit提供方向光的阴影功能,当前只支持开启一个方向光的阴影。
lightCom->SetShadowParam(true, 2);
{
"name": "ShadowMap",
"type": "opaque",
"level": "level",
"tag": "tag",
"passes": [
{
"name": "MainPass",
"rasterizationState": {
"cullMode": "CULL_MODE_NONE",
"fillMode": "FILL_MODE_FILL",
"frontFace": "FRONT_FACE_CLOCKWISE",
"scissorEnable": false,
"lineWidth": 1,
"pointSize": 1
},
"depthStencilState": {
"depthFunc": "COMPARISON_OPERATION_LESS_OR_EQUAL",
"depthTestEnable": true,
"depthClampEnable": false,
"depthWriteEnable": true,
"stencilTestEnable": false,
"front": {
"stencilFail": "STENCIL_OPERATION_KEEP",
"stencilDepthFail": "STENCIL_OPERATION_KEEP",
"stencilPass": "STENCIL_OPERATION_KEEP",
"stencilFunc": "COMPARISON_OPERATION_NOT_EQUAL",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
},
"back": {
"stencilFail": "STENCIL_OPERATION_KEEP",
"stencilDepthFail": "STENCIL_OPERATION_KEEP",
"stencilPass": "STENCIL_OPERATION_KEEP",
"stencilFunc": "COMPARISON_OPERATION_NOT_EQUAL",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
}
},
"colorBlendState": {
"logicOpEnable": false,
"logicOp": "LOGIC_OP_CLEAR",
"blendAttachmentState": [
{
"srcColorBlend": "BLEND_SRC_ALPHA",
"dstColorBlend": "BLEND_ONE_MINUS_SRC_ALPHA",
"colorBlendOp": "BLEND_OPERATION_ADD",
"srcAlphaBlend": "BLEND_ONE",
"dstAlphaBlend": "BLEND_ZERO",
"alphaBlendOp": "BLEND_OPERATION_ADD",
"renderTargetWriteMask": 15,
"blendFactor": 1,
"blendingEnable": false
}
]
},
"resource": {
"shader": [
{
"uri": "shaders/shadowMap.vert.spv",
"type": "SHADER_STAGE_TYPE_VERTEX"
},
{
"uri": "shaders/shadowMap.frag.spv",
"type": "SHADER_STAGE_TYPE_FRAGMENT"
}
]
}
}
]
}{
"name": "ShadowForward",
"type": "opaque",
"level": "level",
"tag": "tag",
"passes": [
{
"name": "MainPass",
"rasterizationState": {
"cullMode": "CULL_MODE_NONE",
"fillMode": "FILL_MODE_FILL",
"frontFace": "FRONT_FACE_CLOCKWISE",
"scissorEnable": false,
"lineWidth": 1,
"pointSize": 1
},
"depthStencilState": {
"depthFunc": "COMPARISON_OPERATION_LESS_OR_EQUAL",
"depthTestEnable": true,
"depthClampEnable": false,
"depthWriteEnable": true,
"stencilTestEnable": false,
"front": {
"stencilFail": "STENCIL_OPERATION_KEEP",
"stencilDepthFail": "STENCIL_OPERATION_KEEP",
"stencilPass": "STENCIL_OPERATION_KEEP",
"stencilFunc": "COMPARISON_OPERATION_NOT_EQUAL",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
},
"back": {
"stencilFail": "STENCIL_OPERATION_KEEP",
"stencilDepthFail": "STENCIL_OPERATION_KEEP",
"stencilPass": "STENCIL_OPERATION_KEEP",
"stencilFunc": "COMPARISON_OPERATION_NOT_EQUAL",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
}
},
"colorBlendState": {
"logicOpEnable": false,
"logicOp": "LOGIC_OP_CLEAR",
"blendAttachmentState": [
{
"srcColorBlend": "BLEND_SRC_ALPHA",
"dstColorBlend": "BLEND_ONE_MINUS_SRC_ALPHA",
"colorBlendOp": "BLEND_OPERATION_ADD",
"srcAlphaBlend": "BLEND_ONE",
"dstAlphaBlend": "BLEND_ZERO",
"alphaBlendOp": "BLEND_OPERATION_ADD",
"renderTargetWriteMask": 15,
"blendFactor": 1,
"blendingEnable": false
}
]
},
"resource": {
"shader": [
{
"uri": "shaders/forward_shadow_pbr.vert.spv",
"type": "SHADER_STAGE_TYPE_VERTEX"
},
{
"uri": "shaders/forward_shadow_pbr.frag.spv",
"type": "SHADER_STAGE_TYPE_FRAGMENT"
}
]
}
}
]
}{
"name": "DeferredShadow",
"type": "opaque",
"level": "level",
"tag": "tag",
"passes": [
{
"name": "Composition",
"rasterizationState": {
"cullMode": "CULL_MODE_BACK",
"fillMode": "FILL_MODE_FILL",
"frontFace": "FRONT_FACE_COUNTER_CLOCKWISE",
"scissorEnable": false,
"lineWidth": 1,
"pointSize": 1
},
"depthStencilState": {
"depthFunc": "COMPARISON_OPERATION_LESS_OR_EQUAL",
"depthTestEnable": true,
"depthClampEnable": false,
"depthWriteEnable": false,
"stencilTestEnable": true,
"front": {
"stencilFail": "STENCIL_OPERATION_REPLACE",
"stencilDepthFail": "STENCIL_OPERATION_REPLACE",
"stencilPass": "STENCIL_OPERATION_REPLACE",
"stencilFunc": "COMPARISON_OPERATION_ALWAYS",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
},
"back": {
"stencilFail": "STENCIL_OPERATION_REPLACE",
"stencilDepthFail": "STENCIL_OPERATION_REPLACE",
"stencilPass": "STENCIL_OPERATION_REPLACE",
"stencilFunc": "COMPARISON_OPERATION_ALWAYS",
"stencilCompareMask": 255,
"stencilReference": 1,
"stencilReadMask": 255,
"stencilWriteMask": 255
}
},
"colorBlendState": {
"logicOpEnable": false,
"logicOp": "LOGIC_OP_CLEAR",
"blendAttachmentState": [
{
"srcColorBlend": "BLEND_SRC_ALPHA",
"dstColorBlend": "BLEND_ONE_MINUS_SRC_ALPHA",
"colorBlendOp": "BLEND_OPERATION_ADD",
"srcAlphaBlend": "BLEND_ONE",
"dstAlphaBlend": "BLEND_ZERO",
"alphaBlendOp": "BLEND_OPERATION_ADD",
"renderTargetWriteMask": 15,
"blendFactor": 1,
"blendingEnable": false
}
]
},
"resource": {
"shader": [
{
"uri": "shaders/composition_shadow.frag",
"type": "SHADER_STAGE_TYPE_FRAGMENT"
},
{
"uri": "shaders/composition.vert",
"type": "SHADER_STAGE_TYPE_VERTEX"
}
]
}
}
]
}#version 430
layout(location=0) in vec3 inPosition;
layout(location=1) in vec2 inTexcoord0;
layout(location=2) in vec3 inNormal;
layout(location=3) in vec4 inTangent;
// pass via specialization constant
#define SHADOW_MAP_CASCADE_COUNT 4
layout(push_constant) uniform CascadeIndex {
uint cascadeIndex;
} cascadeIndex;
layout (set = 1, binding = 1) uniform CascadeViewProjMatrixUniform {
mat4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
vec4 cascadeSplits[SHADOW_MAP_CASCADE_COUNT];
} cascadeViewProjMatrixUniform;
layout(set=0, binding=7) uniform ModelUniform
{
mat4 model;
} modelUniform;
void main()
{
gl_Position = cascadeViewProjMatrixUniform.cascadeViewProjMat[cascadeIndex.cascadeIndex] * modelUniform.model * vec4(inPosition, 1.0);
}#version 430 core
void main()
{
}#version 430 core
precision highp float;
precision mediump sampler2D;
const float PI = 3.141592;
const float Epsilon = 0.00001;
layout(set=0, binding=1) uniform sampler2D albedoTexture;
layout(set=0, binding=2) uniform sampler2D normalTexture;
layout(set=0, binding=3) uniform sampler2D pbrTexture;
layout(set=0, binding=4) uniform sampler2D BRDFTexture;
layout(set=0, binding=5) uniform samplerCube envTexture;
layout(set=0, binding=8) uniform sampler2D emissionTexture;
layout(set=0, binding=9) uniform sampler2DArray shadowMap;
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inTexcoord0;
layout (location = 2) in vec3 inEyePosition;
layout (location = 3) in mat3 inTangentBasis;
layout (location = 0) out vec4 color;
#define SHADOW_MAP_CASCADE_COUNT 4
#define ambientValue 0.3
const mat4 biasMat = mat4(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0
);
// Constant normal incidence Fresnel factor for all dielectrics.
const vec3 Fdielectric = vec3(0.220916301);
layout (set = 1, binding = 1) uniform CascadeViewProjMatrixUniform {
mat4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
vec4 cascadeSplits[SHADOW_MAP_CASCADE_COUNT];
float cascadeCount;
} cascadeViewProjMatrixUniform;
layout(set=0, binding=0) uniform GlobalUniform
{
mat4 view;
mat4 projection;
mat4 projectionOrtho;
mat4 viewProjection;
mat4 viewProjectionInv;
mat4 viewProjectionOrtho;
vec4 resolution;
vec4 cameraPosition;
} globalUniform;
layout(set=0, binding=7) uniform ModelUniform
{
mat4 model;
} modelUniform;
layout (push_constant) uniform LightInfo{
vec4 color;
vec4 position;
vec4 direction;
vec4 typeAngle;
vec4 attenuation;
vec4 ambient;
vec4 diffuse;
vec4 specular;
} lightInfo;
vec4 getAlbedoChannel()
{
return texture(albedoTexture, inTexcoord0);
}
vec4 getNormalChannel()
{
return texture(normalTexture, inTexcoord0);
}
vec4 getPbrChannel()
{
return texture(pbrTexture, inTexcoord0);
}
vec4 getEmissionChannel()
{
return texture(emissionTexture, inTexcoord0);
}
vec4 getBRDFChannel(vec2 location)
{
return texture(BRDFTexture, location);
}
vec4 getEnvChannel(vec3 location, float LOD)
{
return textureLod(envTexture, location, LOD);
}
float ndfGGX(float nh, float roughness)
{
float alphaSq = roughness * roughness;
float denom = (nh * nh) * (alphaSq - 1.0) + 1.0;
return alphaSq / (PI * denom * denom);
}
float gaSchlickG1(float cosTheta, float k)
{
return cosTheta / (cosTheta * (1.0 - k) + k);
}
float gaSchlickGGX(float nl, float nv, float roughness)
{
float r = roughness + 1.0;
// Epic suggests using this roughness remapping for analytic lights.
float k = (r * r) / 8.0;
return gaSchlickG1(nl, k) * gaSchlickG1(nv, k);
}
vec3 fresnelSchlick(vec3 F0, float cosTheta)
{
return F0 + (vec3(1.0) - F0) * pow(1.0 - cosTheta, 5.0);
}
vec3 FresnelLerp(vec3 F0, vec3 F90, float cosA)
{
// ala Schlick interpoliation
float t = pow(1 - cosA, 5.0);
return mix(F0, F90, t);
}
float DisneyDiffuse(float NdotV, float NdotL, float LdotH, float perceptualRoughness)
{
float fd90 = 0.5 + 2 * LdotH * LdotH * perceptualRoughness;
// Two schlick fresnel term
float lightScatter = (1 + (fd90 - 1) * pow((1 - NdotL),5.0));
float viewScatter = (1 + (fd90 - 1) * pow((1 - NdotV),5.0));
return lightScatter * viewScatter;
}
float textureProj(vec4 shadowCoord, vec2 offset, uint cascadeIndex)
{
float shadow = 1.0;
float bias = 0.005;
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 ) {
float dist = texture(shadowMap, vec3(shadowCoord.st + offset, cascadeIndex)).r;
if (shadowCoord.w > 0 && dist < shadowCoord.z - bias) {
shadow = ambientValue;
}
}
return shadow;
}
float filterPCF(vec4 sc, uint cascadeIndex)
{
ivec2 texDim = textureSize(shadowMap, 0).xy;
float scale = 0.75;
float dx = scale * 1.0 / float(texDim.x);
float dy = scale * 1.0 / float(texDim.y);
float shadowFactor = 0.0;
int count = 0;
int range = 1;
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
shadowFactor += textureProj(sc, vec2(dx*x, dy*y), cascadeIndex);
count++;
}
}
return shadowFactor / count;
}
void main()
{
// Sample input textures to get shading model params.
vec4 albedoTex = getAlbedoChannel();
vec3 albedo = albedoTex.rgb;
vec3 orm = getPbrChannel().rgb;
float ao = orm.x;
float perceptualRoughness = orm.y;
float metalness = orm.z;
float roughness = perceptualRoughness * perceptualRoughness;
// Outgoing light direction (vector from world-space fragment position to the "eye").
vec3 eyePosition = inEyePosition;
vec3 Lo = normalize(eyePosition - inPosition);
// Get current fragment's normal and transform to world space.
vec3 N = normalize(2.0 * getNormalChannel().rgb - 1.0);
N = normalize(inTangentBasis[0] * N.x + inTangentBasis[1] * N.y + inTangentBasis[2] * N.z);
// Angle between surface normal and outgoing light direction.
float nv = max(0.0, dot(N, Lo));
// Specular reflection vector.
vec3 Lr = reflect(-Lo, N);
// Fresnel reflectance at normal incidence (for metals use albedo color).
vec3 F0 = mix(Fdielectric, albedo, metalness);
// Direct lighting calculation for analytical lights.
vec3 directLighting = vec3(0);
// Get diffuse contribution factor (as with direct lighting).
float kd = (1 - metalness) * 0.779083699;
#ifdef ENABLE_LIGHT
vec3 Li = vec3(0,0,1);
vec3 Lradiance = lightInfo.color.xyz * lightInfo.color.w;
Li = -normalize(lightInfo.direction.xyz);
// Half-vector between Li and Lo.
vec3 Lh = normalize(Li + Lo);
// Calculate angles between surface normal and various light vectors.
float nl = max(0.0, dot(N, Li));
float nh = max(0.0, dot(N, Lh));
float hl = max(0.0, dot(Lh, Li));
float diffuseTerm = DisneyDiffuse(nv, nl, hl, perceptualRoughness);
vec3 diffuseBRDF = kd * albedo * diffuseTerm;
vec3 F = fresnelSchlick(F0, hl);
float D = ndfGGX(nh, roughness);
float G = gaSchlickGGX(nl, nv, roughness);
float specularTerm = (D * G) * PI / max(Epsilon, 4.0 * nl * nv);
specularTerm = sqrt(max(0.0001, specularTerm));
vec3 specularBRDF = (F * specularTerm);
directLighting += (diffuseBRDF + specularBRDF) * Lradiance * nl;
#endif
// Ambient lighting (IBL).
vec3 ambientLighting;
{
// Sample diffuse irradiance at normal direction.
vec3 irradiance = getEnvChannel(N, 10.0).rgb;
// Calculate Fresnel term for ambient lighting.
// Since we use pre-filtered cubemap(s) and irradiance is coming from many directions
// use nv instead of angle with light's half-vector (nh above).
// See: https://seblagarde.wordpress.com/2011/08/17/hello-world/
vec3 F = fresnelSchlick(F0, nv);
// Irradiance map contains exitant radiance assuming Lambertian BRDF, no need to scale by 1/PI here either.
vec3 diffuseIBL = kd * albedo * irradiance;
// Sample pre-filtered specular reflection environment at correct mipmap level.
vec3 specularIrradiance = getEnvChannel(Lr, perceptualRoughness * 4.0).rgb;
// Split-sum approximation factors for Cook-Torrance specular BRDF.
vec2 specularBRDF = getBRDFChannel(vec2(nv, roughness)).rg;
// Total specular IBL contribution.
float surfaceReduction = 1.0 - 0.28 * roughness * perceptualRoughness;
float smoothness = 1 - perceptualRoughness;
float grazingTerm = clamp((smoothness + (1 - kd)), 0.0, 1.0);
vec3 specularIBL = surfaceReduction * specularIrradiance * FresnelLerp(F0, vec3(grazingTerm), nv);
// Total ambient lighting contribution.
ambientLighting = diffuseIBL + specularIBL;
}
vec4 emission = getEmissionChannel();
// Final fragment color.
float alpha = 1 - kd + albedoTex.a * kd;
color = vec4(directLighting + ambientLighting * ao + emission.rgb, alpha);
vec3 inViewPos = (globalUniform.view * vec4(inPosition, 1.0)).xyz;
uint cascadeIndex = 0;
for(uint i = 0; i < cascadeViewProjMatrixUniform.cascadeCount - 1; ++i) {
if(cascadeViewProjMatrixUniform.cascadeSplits[i].x < inViewPos.z) {
cascadeIndex = i + 1;
}
}
// Depth compare for shadowing
vec4 shadowCoord = (biasMat * cascadeViewProjMatrixUniform.cascadeViewProjMat[cascadeIndex]) * vec4(inPosition, 1.0);
float shadow = 0;
shadow = filterPCF(shadowCoord / shadowCoord.w, cascadeIndex);
color.rgb *= shadow;
color.a = color.a;
}lightCom->SetShadowParam(false, 2);