Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions assets/DMonkey/AmbientLight.j3md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ MaterialDef AmbientLight {
Texture2D DepthBuffer
Texture2D NormalBuffer
Texture2D DiffuseBuffer
Vector3 LightPosition

Vector4Array DirectionalColors
Vector3Array Directions

Float LightIntensity
Int DirectionalLights

}

Expand All @@ -15,7 +19,10 @@ MaterialDef AmbientLight {
FragmentShader GLSL100: DMonkey/Shaders/AmbientLight.frag

WorldParameters {
Resolution
ViewMatrix
ProjectionMatrixInverse
WorldViewProjectionMatrix
Resolution
}
}

Expand Down
18 changes: 4 additions & 14 deletions assets/DMonkey/GBuffer.j3md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ MaterialDef GBuffer {
FragmentShader GLSL100: DMonkey/Shaders/GBuffer.frag

WorldParameters {
ProjectionMatrixInverse
WorldViewProjectionMatrix
WorldViewNatrix
ViewMatrix
ViewMatrixInverse
NormalMatrix
WorldMatrixInverse
WorldMatrix
ViewMatrix
NormalMatrix
FrustumNearFar
Time
}
Expand All @@ -41,14 +36,9 @@ MaterialDef GBuffer {
FragmentShader GLSL100: DMonkey/Shaders/GBuffer.frag

WorldParameters {
ProjectionMatrixInverse
WorldViewProjectionMatrix
WorldViewNatrix
ViewMatrix
ViewMatrixInverse
NormalMatrix
WorldMatrixInverse
WorldMatrix
ViewMatrix
NormalMatrix
FrustumNearFar
Time
}
Expand Down
22 changes: 8 additions & 14 deletions assets/DMonkey/PointLight.j3md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
MaterialDef PointLight {

MaterialParameters {
Vector4 LightColor
Texture2D DepthBuffer
Texture2D NormalBuffer
Texture2D DiffuseBuffer

Vector4 LightColor
Vector3 LightPosition
Float LightIntensity
Float LightRadius
Boolean fullscreen
Vector3 FrustumCorner
Vector2 FrustumNearFar

Float LightIntensity

Boolean fullscreen
Boolean specular : true
}

Expand All @@ -20,22 +21,15 @@ MaterialDef PointLight {

WorldParameters {
ViewMatrix
ProjectionMatrixInverse
ViewProjectionMatrixInverse
ProjectionMatrixInverse
WorldViewProjectionMatrix
WorldViewProjectionMatrixInverse
ViewProjectionMatrixInverse
Resolution
CameraDirection
}
Defines {
NORMAL_BUFFER : NormalBuffer
DIFFUSE_BUFFER : DiffuseBuffer
DEPTH_BUFFER : DepthBuffer
FULLSCREEN : fullscreen
SPECULAR : specular
}
}

Technique DeferredLight{
VertexShader GLSL100: Shaders/PointLight.vert
FragmentShader GLSL100: Shaders/PointLight.frag
Expand Down
33 changes: 20 additions & 13 deletions assets/DMonkey/Shaders/AmbientLight.frag
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
#import "DMonkey/Shaders/GBuffer.glsllib"
#import "DMonkey/Shaders/Gamma.glsllib"
#import "DMonkey/Shaders/DM_Light.glsllib"
uniform vec4 m_Color;

uniform vec3 g_CameraDirection;
uniform vec2 g_Resolution;
uniform mat4 g_WorldViewProjectionMatrixInverse;

uniform vec3 m_LightPosition;
uniform float m_LightIntensity;

uniform int m_DirectionalLights;
uniform vec4 m_DirectionalColors[12];
uniform vec3 m_Directions[12];

#define EXTRACT_DEPTH(cc)((cc).b + (cc).g / 256.0 + (cc).r / (256.0 * 256.0) + (cc).a / (256.0 * 256.0 * 256.0))

void main() {
// Compute GBuffer sample position
vec2 TexCoord = gl_FragCoord.xy / g_Resolution;
dm_decode(TexCoord);

// Prepare
prepare();

vec3 albedo = gamma(GBuffer.albedo, 2.2);
//gl_FragColor.rgb = 0.05 * albedo;

float mixValue = dot(GBuffer.normal, vec3(0.0,1.0,0.0))*0.5 + 0.5;
gl_FragColor.rgb = mix(vec3(0.3,0.3,0.8)*0.5, vec3(0.7,0.7,1), mixValue) * albedo * 0.2;
vec4 dirColors;

for(int i = 0; i < 3; i++) {
vec3 dir = -(g_ViewMatrix * vec4(m_Directions[i], 0)).xyz;

Light light = Light(m_DirectionalColors[i], dir, dir);
vec4 color = light.color * vec4(gamma(ComputeLighting(light).rgb, 2.2), 1);

dirColors += color;
}

vec4 ambient = m_Color;
gl_FragColor.rgb = ambient.rgb * albedo + dirColors.rgb * albedo;
}

48 changes: 48 additions & 0 deletions assets/DMonkey/Shaders/DM_Light.glsllib
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#import "DMonkey/Shaders/GBuffer.glsllib"

vec3 gamma(vec3 color, float gamma){
color.r = pow(color.r, gamma);
color.g = pow(color.g, gamma);
color.b = pow(color.b, gamma);
return color;
}

struct Light {
vec4 color;
vec3 position;

vec3 direction;
};

vec4 ComputeLighting(Light light) {

float lambert = clamp(dot(GBuffer.normal, light.direction), 0.0, 1.0);

vec4 DiffuseColor = vec4(0, 0, 0, 0);
vec4 SpecularColor = vec4(0, 0, 0, 0);

if (lambert > 0.0) {
DiffuseColor = light.color * lambert;

#ifdef SPECULAR
vec3 vsCameraDir = normalize(GBuffer.position);
vec3 reflection = reflect(light.direction, GBuffer.normal);
float specular = clamp(dot(normalize(vsCameraDir), normalize(reflection)), 0.0, 1.0);
specular = pow(specular, 120.0);

if (specular > 0.0) {
SpecularColor = vec4(light.color.rgb, 1.0) * GBuffer.specular * specular;
}
#else
float specular = 0.0;
#endif
}

return DiffuseColor + SpecularColor;
}

// Other uses.. later on
void prepare() {
// Do it here
dm_decode();
}
40 changes: 19 additions & 21 deletions assets/DMonkey/Shaders/GBuffer.glsllib
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#define DM_NORMAL_BUFFER m_NormalBuffer
#define DM_DIFFUSE_BUFFER m_DiffuseBuffer
#define DM_DEPTH_BUFFER m_DepthBuffer

uniform sampler2D DM_NORMAL_BUFFER;
uniform sampler2D DM_DIFFUSE_BUFFER;
uniform sampler2D DM_DEPTH_BUFFER;


/**
* These uniform should be passed in from JME;
*/
uniform mat4 g_ViewProjectionMatrixInverse;
// Global uniforms from JME
uniform mat4 g_ProjectionMatrixInverse;
uniform mat4 g_ViewMatrix;
uniform vec2 g_Resolution;

// MRT Textures
uniform sampler2D m_NormalBuffer;
uniform sampler2D m_DiffuseBuffer;
uniform sampler2D m_DepthBuffer;

// Material
uniform vec3 m_FrustumCorner;
uniform vec2 m_FrustumNearFar;

Expand All @@ -26,7 +22,7 @@ struct DM_GBuffer{
} GBuffer;

float dm_depth(vec2 tc){
return texture2D(DM_DEPTH_BUFFER, tc).r;
return texture2D(m_DepthBuffer, tc).r;
}

vec3 dm_position(vec2 tc, float projectiveDepth){
Expand All @@ -36,7 +32,7 @@ vec3 dm_position(vec2 tc, float projectiveDepth){
}

vec3 dm_normal(vec2 tc){
return normalize((texture2D(DM_NORMAL_BUFFER, tc) * 2.0 - 1.0).rgb);
return normalize((texture2D(m_NormalBuffer, tc) * 2.0 - 1.0).rgb);
}

/**
Expand All @@ -48,19 +44,21 @@ float dm_non_linear_2_linear_depth(float w){
return (w - wnear) * (wfar / (wfar - wnear));
}

void dm_decode(vec2 tc){
void dm_decode() {
vec2 tc = gl_FragCoord.xy/g_Resolution;

GBuffer.depth = dm_depth(tc);
GBuffer.linear_depth = dm_non_linear_2_linear_depth(GBuffer.depth);

// Decode normal and specular
vec4 normalAndSpecluar = texture2D(DM_NORMAL_BUFFER, tc);
vec4 normalAndSpecluar = texture2D(m_NormalBuffer, tc);
GBuffer.normal = normalAndSpecluar.rgb * 2.0 - 1.0;
GBuffer.specular = normalAndSpecluar.a;

// Decode position
GBuffer.position = dm_position(tc, GBuffer.depth);
// Decode position
GBuffer.position = dm_position(tc, GBuffer.depth);

// Recover albedo
GBuffer.albedo = texture2D(DM_DIFFUSE_BUFFER, tc).rgb;
// Recover albedo
GBuffer.albedo = texture2D(m_DiffuseBuffer, tc).rgb;
}

69 changes: 16 additions & 53 deletions assets/DMonkey/Shaders/PointLight.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#import "DMonkey/Shaders/GBuffer.glsllib"
#import "DMonkey/Shaders/Gamma.glsllib"
#import "DMonkey/Shaders/DM_Light.glsllib"

// Global uniforms from JME
uniform mat4 g_WorldViewProjectionMatrixInverse;
uniform vec3 g_CameraDirection;
uniform vec2 g_Resolution;

// Light parameters
// Point Light parameters
uniform vec3 m_LightPosition;
uniform float m_LightIntensity;
uniform float m_LightRadius;
Expand All @@ -22,58 +16,27 @@ float quad_fallof(float D, float r, float kq){
float fallof(float D, float r, float kl, float kq){
return linear_fallof(D, r, kl) * quad_fallof(D, r, kq);
}
vec4 perform_lighting(vec3 Position, vec3 LightPos, vec3 Normal, vec3 Albedo, vec4 LightColor, float LightRadius){
vec4 color = vec4(0);

vec3 LightVector = LightPos - Position;
vec3 LightDir = normalize(LightVector);

float lambert = clamp(dot(GBuffer.normal, LightDir), 0.0, 1.0);

#ifdef SPECULAR
vec3 vsCameraDir = normalize(GBuffer.position);
vec3 reflection = reflect(LightDir, GBuffer.normal);
float specular = clamp(dot(normalize(Position), normalize(reflection)), 0.0, 1.0);
specular = pow(specular, 120.0)*GBuffer.specular;
#else
float specular = 0.0;
#endif
void main() {

// Decode gbuffer
prepare();



// Compute distance to light
float Dist = length(LightVector);
float fallof = fallof(LightRadius, Dist, 0.0, 1.0);
fallof *= LightColor.a;
vec3 LightVector = m_LightPosition - GBuffer.position;
vec3 LightDir = normalize(LightVector);

color = LightColor*(lambert + specular) * fallof;
return color;
}
Light light = Light(m_LightColor, m_LightPosition, LightDir);

vec4 color = light.color * ComputeLighting(light);

void main() {
vec2 TexCoord = gl_FragCoord.xy/g_Resolution;

// Decode gbuffer
dm_decode(TexCoord);



// Get view space camera direction
//vec3 vsCameraDir = normalize(GBuffer.position);
float Dist = length(LightVector);
float fallof = fallof(m_LightRadius, Dist, 0.0, 1.0);

vec4 albedo = vec4(GBuffer.albedo, 1.0);

// GammaCorrect textures
albedo.rgb = gamma(albedo.rgb, 2.2);



gl_FragColor = perform_lighting(GBuffer.position, m_LightPosition, GBuffer.normal, albedo.rgb, m_LightColor, m_LightRadius);
gl_FragColor = color * fallof * light.color.a;

//gl_FragColor += 0.03;
gl_FragColor *= albedo;

return;
// GammaCorrect textures
vec3 albedo = gamma(GBuffer.albedo, 2.2);
gl_FragColor *= vec4(albedo, 1);
}

4 changes: 2 additions & 2 deletions assets/DMonkey/Shaders/Resolve.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ uniform mat4 m_ProjectionMatrix;
varying vec2 TexCoord;

vec3 FilmicMain(vec3 texColor){
float range = 0.5;
float range = 0.2;
texColor *= range;
vec3 x = max(vec3(0.0),texColor-0.004); // Filmic Curve
vec3 retColor = (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);
Expand All @@ -21,5 +21,5 @@ void main() {
gl_FragColor.rgb = LBuffer.rgb;
//gl_FragColor.rgb = gl_FragColor.rgb / (1.0 + gl_FragColor.rgb);
//gl_FragColor.rgb = gamma(gl_FragColor.rgb, 1.0/2.2);
gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb);
//gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb);
}
Loading