Skip to content

Commit

Permalink
volumetric: make it works for tcinstanced objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vlj committed Jan 14, 2024
1 parent b9b1840 commit 790b8d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
17 changes: 12 additions & 5 deletions data/base/shaders/vk/tcmask_instanced.frag
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ layout(location = 12) in vec3 fragPos;

layout(location = 0) out vec4 FragColor;

#include "pointlights.glsl"

float getShadowMapDepthComp(vec2 base_uv, float u, float v, vec2 shadowMapSizeInv, int cascadeIndex, float z)
{
vec2 uv = base_uv + vec2(u, v) * shadowMapSizeInv;
return texture( shadowMap, vec4(uv, cascadeIndex, z) );
}

float getShadowVisibility()
float getShadowVisibility(vec3 fragPos)
{
if (WZ_SHADOW_MODE == 0 || WZ_SHADOW_FILTER_SIZE == 0)
{
Expand Down Expand Up @@ -264,6 +262,9 @@ float getShadowVisibility()
}
}


#include "pointlights.glsl"

vec3 blendAddEffectLighting(vec3 a, vec3 b) {
return min(a + b, vec3(1.0));
}
Expand Down Expand Up @@ -305,7 +306,7 @@ void main()
vec4 light = sceneColor;
vec3 L = normalize(lightDir);
float lambertTerm = max(dot(N, L), 0.0); //diffuse light
float visibility = getShadowVisibility();
float visibility = getShadowVisibility(fragPos);
vec4 lightmap_vec4 = texture(lightmap_tex, uvLightmap.xy, 0.f);
float distanceAboveTerrain = uvLightmap.z;
float lightmapFactor = 1.0f - (clamp(distanceAboveTerrain, 0.f, 300.f) / 300.f);
Expand Down Expand Up @@ -371,7 +372,12 @@ void main()
{
fragColour.a = 0.66 + 0.66 * graphicsCycle;
}


#if 1
vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight);
vec4 volumetric = volumetricIterateOverAllPointLights(clipSpaceCoord, cameraPos.xyz, fragPos);
fragColour.xyz = toneMap(fragColour.xyz * volumetric.a + volumetric.xyz);
#else
if (fogEnabled > 0)
{
// Calculate linear fog
Expand All @@ -381,6 +387,7 @@ void main()
// Return fragment color
fragColour = mix(fragColour, vec4(fogColor.xyz, fragColour.w), fogFactor);
}
#endif

FragColor = fragColour;
}
1 change: 1 addition & 0 deletions data/base/shaders/vk/tcmask_instanced.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ layout(std140, set = 0, binding = 0) uniform globaluniforms
vec4 PointLightsColorAndEnergy[WZ_MAX_POINT_LIGHTS];
ivec4 bucketOffsetAndSize[WZ_BUCKET_DIMENSION * WZ_BUCKET_DIMENSION];
ivec4 PointLightsIndex[WZ_MAX_INDEXED_POINT_LIGHTS];
vec4 cameraPos; // in modelSpace
};

layout(std140, set = 1, binding = 0) uniform meshuniforms
Expand Down
1 change: 1 addition & 0 deletions lib/ivis_opengl/gfx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ namespace gfx_api
std::array<glm::vec4, max_lights> PointLightsColorAndEnergy;
std::array<glm::ivec4, bucket_dimension * bucket_dimension> bucketOffsetAndSize;
std::array<glm::ivec4, max_indexed_lights> indexed_lights;
glm::vec4 cameraPos;
};

// Only change per mesh
Expand Down
24 changes: 15 additions & 9 deletions lib/ivis_opengl/piedraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,29 +1383,35 @@ bool InstancedMeshRenderer::DrawAll(uint64_t currentGameFrame, const glm::mat4&
glm::vec4 specular(lighting0[LIGHT_SPECULAR][0], lighting0[LIGHT_SPECULAR][1], lighting0[LIGHT_SPECULAR][2], lighting0[LIGHT_SPECULAR][3]);

const auto &renderState = getCurrentRenderState();
const glm::vec4 fogColor = renderState.fogEnabled ? glm::vec4(
renderState.fogColour.vector[0] / 255.f,
renderState.fogColour.vector[1] / 255.f,
renderState.fogColour.vector[2] / 255.f,
renderState.fogColour.vector[3] / 255.f
) : glm::vec4(0.f);


if (useInstancedRendering)
{

const glm::vec3 cameraPos = (glm::inverse(viewMatrix) * glm::vec4(0, 0, 0, 1)).xyz();
const glm::vec4 fogColor = glm::vec4(
renderState.fogColour.vector[0] / 255.f,
renderState.fogColour.vector[1] / 255.f,
renderState.fogColour.vector[2] / 255.f,
renderState.fogColour.vector[3] / 255.f
);
auto bucketLight = getCurrentLightingManager().getPointLightBuckets();
auto dimension = gfx_api::context::get().getDrawableDimensions();
gfx_api::Draw3DShapeInstancedGlobalUniforms globalUniforms {
projectionMatrix, viewMatrix, modelUVLightmapMatrix, {shadowCascades.shadowMVPMatrix[0], shadowCascades.shadowMVPMatrix[1], shadowCascades.shadowMVPMatrix[2]},
glm::vec4(currentSunPosition, 0.f), sceneColor, ambient, diffuse, specular, fogColor,
{shadowCascades.shadowCascadeSplit[0], shadowCascades.shadowCascadeSplit[1], shadowCascades.shadowCascadeSplit[2], pie_getPerspectiveZFar()}, shadowCascades.shadowMapSize,
renderState.fogBegin, renderState.fogEnd, pie_GetShaderTime(), renderState.fogEnabled, static_cast<int>(dimension.first), static_cast<int>(dimension.second), 0.f, bucketLight.positions, bucketLight.colorAndEnergy, bucketLight.bucketOffsetAndSize, bucketLight.light_index
renderState.fogBegin, renderState.fogEnd, pie_GetShaderTime(), renderState.fogEnabled, static_cast<int>(dimension.first), static_cast<int>(dimension.second), 0.f, bucketLight.positions, bucketLight.colorAndEnergy, bucketLight.bucketOffsetAndSize, bucketLight.light_index,
glm::vec4(cameraPos, 0.f)
};
Draw3DShapes_Instanced(currentGameFrame, perFrameUniformsShaderOnce, globalUniforms, drawParts, depthPass);
}
else
{
const glm::vec4 fogColor = renderState.fogEnabled ? glm::vec4(
renderState.fogColour.vector[0] / 255.f,
renderState.fogColour.vector[1] / 255.f,
renderState.fogColour.vector[2] / 255.f,
renderState.fogColour.vector[3] / 255.f
) : glm::vec4(0.f);
gfx_api::Draw3DShapeGlobalUniforms globalUniforms {
projectionMatrix, viewMatrix, shadowCascades.shadowMVPMatrix[0],
glm::vec4(currentSunPosition, 0.f), sceneColor, ambient, diffuse, specular, fogColor,
Expand Down

0 comments on commit 790b8d2

Please sign in to comment.