Skip to content

Commit

Permalink
Reworked built-in shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
adriengivry committed Mar 20, 2024
1 parent 8b39ca1 commit 79e51d7
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 358 deletions.
1 change: 1 addition & 0 deletions Resources/Engine/Shaders/Common/Constants.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const float PI = 3.14159265359;
9 changes: 0 additions & 9 deletions Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh

This file was deleted.

5 changes: 0 additions & 5 deletions Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh

This file was deleted.

9 changes: 0 additions & 9 deletions Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh

This file was deleted.

14 changes: 14 additions & 0 deletions Resources/Engine/Shaders/Common/Physics.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize)
{
return
(
p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x &&
p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y &&
p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z
);
}

bool PointInSphere(vec3 p_Point, vec3 p_SphereCenter, float p_SphereRadius)
{
return distance(p_Point, p_SphereCenter) <= p_SphereRadius;
}
55 changes: 55 additions & 0 deletions Resources/Engine/Shaders/Common/Utils.ovfxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
vec2 TRANSFORM_TEX_COORDS(vec2 texCoords, vec2 tiling, vec2 offset)
{
return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset;
}

vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
{
if (heightScale > 0)
{
const vec3 viewDir = normalize(tangentViewPos - tangentFragPos);
const vec2 parallax = viewDir.xy * heightScale * texture(heightMap, texCoords).r;
return texCoords - vec2(parallax.x, 1.0 - parallax.y);
}

return texCoords;
}

bool IS_MASKED(sampler2D maskMap, vec2 texCoords)
{
return texture(maskMap, texCoords).r == 0.0;
}

mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent)
{
return mat3(
normalize(vec3(model * vec4(tangent, 0.0))),
normalize(vec3(model * vec4(bitangent, 0.0))),
normalize(vec3(model * vec4(normal, 0.0)))
);
}

vec3 UnPack(float p_Target)
{
return vec3
(
float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451,
float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451,
float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451
);
}

vec3 COMPUTE_NORMAL(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN)
{
if (enableNormalMapping)
{
normal = texture(normalMap, texCoords).rgb;
normal = normalize(normal * 2.0 - 1.0);
normal = normalize(TBN * normal);
return normal;
}
else
{
return normalize(normal);
}
}
184 changes: 0 additions & 184 deletions Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh

This file was deleted.

26 changes: 0 additions & 26 deletions Resources/Engine/Shaders/Fragment/Lambert.ovfxh

This file was deleted.

15 changes: 0 additions & 15 deletions Resources/Engine/Shaders/Fragment/Unlit.ovfxh

This file was deleted.

58 changes: 56 additions & 2 deletions Resources/Engine/Shaders/Lambert.ovfx
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
#shader vertex
#include ":Shaders/Vertex/Basic.ovfxh"
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;

out VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
} vs_out;

void main()
{
vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0));
vs_out.TexCoords = geo_TexCoords;
vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal);

gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0);
}

#shader fragment
#include ":Shaders/Fragment/Lambert.ovfxh"
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"
#include ":Shaders/Lighting/Lambert.ovfxh"

in VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
} fs_in;

uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0);
uniform sampler2D u_DiffuseMap;
uniform vec2 u_TextureTiling = vec2(1.0, 1.0);
uniform vec2 u_TextureOffset = vec2(0.0, 0.0);

out vec4 FRAGMENT_COLOR;

void main()
{
vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);

const vec3 kLightPosition = vec3(-9000.0, 10000.0, 11000.0);
const vec3 kLightDiffuse = vec3(1.0);
const vec3 kLightAmbient = vec3(0.3);
const vec3 lambert = Lambert(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient);

FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse * vec4(lambert, 1.0);
}
Loading

0 comments on commit 79e51d7

Please sign in to comment.