DeerEngine/Resources/normal.glsl
2026-02-17 09:33:57 +01:00

83 lines
2.0 KiB
GLSL

#type vertex
#version 410 core
// Vertex attributes
layout(location = 0) in vec3 v_position;
layout(location = 1) in vec3 v_normal;
layout(location = 2) in vec2 v_UV;
// Outputs to fragment shader
out vec3 normalWS;
out vec3 normalVS;
out vec3 positionVS;
// Uniforms
uniform mat4 u_worldMatrix;
uniform mat4 u_viewMatrix;
void main()
{
gl_Position = u_viewMatrix * u_worldMatrix * vec4(v_position, 1.0);
positionVS = gl_Position.xyz;
mat3 normalMatrix = transpose(inverse(mat3(u_worldMatrix)));
normalWS = normalize(normalMatrix * v_normal);
mat3 normalMatrixView = transpose(inverse(mat3(u_viewMatrix * u_worldMatrix)));
normalVS = normalize(normalMatrixView * v_normal);
}
#type fragment
#version 410 core
// Fragment outputs
layout(location = 0) out vec4 fragColor;
layout(location = 1) out int objectID;
// Inputs from vertex shader
in vec3 normalWS;
in vec3 normalVS;
in vec3 positionVS;
// Uniforms
uniform int u_objectID;
void main()
{
// Directional light
vec3 lightDir = normalize(vec3(1.0, 7.0, 3.0));
// Base light intensity
float light = clamp(dot(normalize(normalWS), lightDir) * 0.8 + 0.2, 0.0, 1.0);
// Gradient from purple (dark) to cyan (lit)
vec3 gradientColor = mix(
vec3(0.9412, 0.4471, 0.7137),
vec3(1.0, 0.9725, 0.5255),
light
);
// Rim/fresnel effect
float rim = 0.5 - dot(normalize(normalWS), normalize(lightDir)) * 0.5;
float rimValue = rim * rim * rim * rim;
vec3 rimColor = vec3(1.0, 0.8, 0.5) * (rim * rim * rim * rim);
vec3 mixedColor = mix(
gradientColor,
vec3(1, 1, 1) ,
rimValue
);
//float fresnel = dot(, vec3(0, 0, -1));
//fresnel = fresnel * fresnel;
float fresnel = 1 + dot(normalize(positionVS), normalize(normalVS));
fresnel = fresnel * fresnel * fresnel * fresnel;
// Combine everything
fragColor = vec4(normalize(normalWS) * 0.5 + vec3(0.5, 0.5, 0.5), 1.0);
// Object ID output
objectID = u_objectID;
}