Skip to content

Commit

Permalink
Translucent materials support, blending options for materials, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Mormert committed Aug 9, 2023
1 parent 24750e0 commit b6894ee
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 77 deletions.
50 changes: 37 additions & 13 deletions engine/EngineResources/shaders/defaultMesh.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ uniform bool uUseRoughnessTexture;
uniform sampler2D uRoughnessTexture;
uniform float uRoughness;

uniform bool uUseOpacityTexture;
uniform sampler2D uOpacityTexture;
uniform float uOpacity;

// Does the texture for the opacity consist of a single alpha channel?
uniform bool uOpacityTextureOneChannel;

uniform float uFarPlane;

uniform int uLightsCount;
Expand Down Expand Up @@ -285,12 +292,6 @@ float ShadowCalculationPoint(vec3 fragPos, vec3 lightPos)
{
vec3 worldPosToLight = fragPos - lightPos;

// Sample from the cube shadow texture
float closestDepth = texture(uShadowMapPoint, worldPosToLight).r;

// Re-map from 0 to 1 back to 0 to far plane linearly
closestDepth *= uFarPlane;

// The current depth from the light source
float currentDepth = length(worldPosToLight);

Expand All @@ -300,15 +301,15 @@ float ShadowCalculationPoint(vec3 fragPos, vec3 lightPos)
float offset = 0.1;

// PCF
for(float x = -offset; x < offset; x += offset / (samples * 0.5))
for (float x = -offset; x < offset; x += offset / (samples * 0.5))
{
for(float y = -offset; y < offset; y += offset / (samples * 0.5))
for (float y = -offset; y < offset; y += offset / (samples * 0.5))
{
for(float z = -offset; z < offset; z += offset / (samples * 0.5))
for (float z = -offset; z < offset; z += offset / (samples * 0.5))
{
float closestDepth = texture(uShadowMapPoint, worldPosToLight + vec3(x, y, z)).r;
closestDepth *= uFarPlane; // undo mapping [0;1]
if(currentDepth - bias < closestDepth)
closestDepth *= uFarPlane;// undo mapping [0;1]
if (currentDepth - bias < closestDepth)
{
shadow += 1.0;
}
Expand Down Expand Up @@ -420,8 +421,31 @@ float getRoughness()
}
}

float getOpacity()
{
if (uUseOpacityTexture)
{
// If the texture is an RGBA texture or single alpha channel texture
// Allows for using the albedo/diffuse texture as opacity texture.
if (uOpacityTextureOneChannel){
return texture(uOpacityTexture, TexCoords).r;
} else
{
return texture(uOpacityTexture, TexCoords).a;
}
} else
{
return uOpacity;
}
}

void main()
{
float opacity = getOpacity();
if (opacity < 0.1)
{
discard;
}

vec3 albedo = getAlbedo();
vec3 N = getNormal();
Expand Down Expand Up @@ -482,9 +506,9 @@ void main()
LightOutTotal = LightOutTotal / (LightOutTotal + vec3(1.0));

// Gamma correction
// LightOutTotal = pow(LightOutTotal, vec3(1.0/2.2));
LightOutTotal = pow(LightOutTotal, vec3(1.0/2.2));

FragColor = vec4(LightOutTotal, 1.0);
FragColor = vec4(LightOutTotal, opacity);
}


18 changes: 17 additions & 1 deletion engine/EngineResources/shaders/shadowMapping.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@


layout (location = 0) in vec3 aPos;
layout (location = 2) in vec2 aTexCoords;

uniform mat4 lightSpaceMatrix;
uniform mat4 model;

out vec2 TexCoords;

void main()
{
TexCoords = aTexCoords;
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0);
}

/*BEGIN FRAG*/

#define BIAS 0.1

in vec2 TexCoords;

uniform bool uUseOpacityTexture;
uniform sampler2D uOpacityTexture;

void main()
{
// Not doing anything in this frag shader is the equivalent to:
Expand All @@ -24,8 +32,16 @@ void main()
// and needs to be accounted for in the mesh rendering shader.
// Another way is to account for peter panning in this shadow mapping shader:

// Account for vegetation, etc
if (uUseOpacityTexture)
{
if (texture(uOpacityTexture, TexCoords).a < 0.1) {
discard;
}
}

gl_FragDepth = gl_FragCoord.z;
gl_FragDepth += gl_FrontFacing ? BIAS : 0.0; // Mitigate shadow acne
gl_FragDepth += gl_FrontFacing ? BIAS : 0.0;// Mitigate shadow acne

// The bias could also be calculated depending on surface normal and light direction
// float bias = max(0.05 * (1.0 - dot(N, L)), 0.005);
Expand Down
17 changes: 17 additions & 0 deletions engine/EngineResources/shaders/shadowMappingPoint.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@


layout (location = 0) in vec3 aPos;
layout (location = 2) in vec2 aTexCoords;


uniform mat4 lightSpaceMatrix;
uniform mat4 model;

out vec2 TexCoords;
out vec3 WorldPos;

void main()
{
TexCoords = aTexCoords;
WorldPos = vec3(model * vec4(aPos, 1.0));
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0);
}
Expand All @@ -19,10 +23,23 @@ void main()
uniform float farPlane;
uniform vec3 lightPos;

in vec2 TexCoords;

in vec3 WorldPos;

uniform bool uUseOpacityTexture;
uniform sampler2D uOpacityTexture;

void main()
{
// Account for vegetation, etc
if (uUseOpacityTexture)
{
if (texture(uOpacityTexture, TexCoords).a < 0.1) {
discard;
}
}

float lightDistance = length(WorldPos - lightPos);

// Linearly map the distance from 0 to 1
Expand Down
15 changes: 9 additions & 6 deletions engine/jle3DGraph.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright (c) 2023. Johan Lind


#include "jle3DGraph.h"
#include "jleMaterial.h"
#include "jleMesh.h"

void
jle3DGraph::sendMesh(std::shared_ptr<jleMesh> &mesh,
std::shared_ptr<jleMaterial> &material,
const glm::mat4 &transform,
int instanceId,
bool castShadows)
std::shared_ptr<jleMaterial> &material,
const glm::mat4 &transform,
int instanceId,
bool castShadows)
{
_meshes.emplace_back(jle3DQueuedMesh{mesh, material, transform, instanceId, castShadows});
if (material && material->isTranslucent()) {
_translucentMeshes.emplace_back(jle3DQueuedMesh{mesh, material, transform, instanceId, castShadows});
} else {
_meshes.emplace_back(jle3DQueuedMesh{mesh, material, transform, instanceId, castShadows});
}
}

void
Expand Down
1 change: 1 addition & 0 deletions engine/jle3DGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class jle3DGraph

private:
std::vector<jle3DQueuedMesh> _meshes;
std::vector<jle3DQueuedMesh> _translucentMeshes;
std::vector<std::vector<jle3DLineVertex>> _lineStrips;
std::vector<jle3DLineVertex> _lines;
std::vector<jle3DRendererLight> _lights;
Expand Down
Loading

0 comments on commit b6894ee

Please sign in to comment.