diff --git a/assets/DMonkey/Shaders/AmbientLight.frag b/assets/DMonkey/Shaders/AmbientLight.frag index e95bde1..8dfd292 100644 --- a/assets/DMonkey/Shaders/AmbientLight.frag +++ b/assets/DMonkey/Shaders/AmbientLight.frag @@ -1,10 +1,13 @@ #define SPECULAR #define GAMMA_CORRECT + +uniform float g_Time; #import "DMonkey/Shaders/DM_Light.glsllib" uniform vec4 m_Color; uniform float m_LightIntensity; uniform vec3 m_ViewLightDir; + const vec3 lightDirection = vec3(0.1, 0.3, 0.0); const float GAMMA = 2.2; vec3 FilmicMain(vec3 texColor){ @@ -26,12 +29,13 @@ void main() { Light light = Light(vec4(gamma(m_Color.rgb, GAMMA), 0.0), vec3(0.0), m_ViewLightDir); vec4 color = ComputeLighting(light); - color += vec4(0.05, 0.05, 0.1, 0.0); + //color += vec4(0.05, 0.05, 0.1, 0.0); - gl_FragColor.rgb = albedo * color.rgb * 1.5; + gl_FragColor.rgb = albedo * color.rgb * 10.0; - //gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb); + #ifdef GAMMA_CORRECT + //gl_FragColor.rgb = FilmicMain(gl_FragColor.rgb); gl_FragColor.rgb = gamma(gl_FragColor.rgb, 1.0 / GAMMA); #endif } diff --git a/assets/DMonkey/Shaders/DM_Light.glsllib b/assets/DMonkey/Shaders/DM_Light.glsllib index 05e92d0..cf4e590 100644 --- a/assets/DMonkey/Shaders/DM_Light.glsllib +++ b/assets/DMonkey/Shaders/DM_Light.glsllib @@ -1,5 +1,36 @@ #import "DMonkey/Shaders/GBuffer.glsllib" +const int QUALITY_LOW = 0, + QUALITY_MED = 1, + QUALITY_HIGH = 2; + +const float PI = 3.14159265358979323846264; +const float ONE_OVER_PI = 1.0 / PI; +const float PI_OVER_FOUR = PI / 4.0; +const float PI_OVER_TWO = PI / 2.0; + +// Use this instead of Lambert for the diffuse part +float OrenNayar(vec3 normal, vec3 lightDir, vec3 viewDir, float roughness) +{ + float vdotl = max(0.0, dot(viewDir, lightDir)); + float ndotv = max(0.0, dot(normal, viewDir)); + float ndotl = max(0.0, dot(normal, lightDir)); + + // theta_o = arcsin ndotv (as angle) + // theta_i = arcsin ndotl + float sigma2 = roughness * roughness; + float A = 1.0 - (0.5 * sigma2) / (sigma2 + 0.33); + float B = (0.45 * sigma2) / (sigma2 + 0.09); + float alpha = max(ndotl, ndotv); + float beta = min(ndotl, ndotv); + return (ndotl * ONE_OVER_PI) * (A + B * vdotl * sin(alpha) * tan(beta)); +} + +float Lambert(vec3 normal, vec3 lightDir){ + return max(dot(normal, lightDir), 0.0); +} + + vec3 gamma(vec3 color, float gamma){ color.r = pow(color.r, gamma); color.g = pow(color.g, gamma); @@ -20,12 +51,14 @@ float fastFresnel(vec3 ViewDir, vec3 Normal){ return R0 + (1.0 - R0)*pow((1.0 - dot(ViewDir, Normal)),5.0); } vec4 ComputeLighting(Light light) { + float Roughness = 128.0; - float lambert = max(dot(GBuffer.normal, light.direction), 0.0); + float lambert = Lambert(GBuffer.normal, light.direction); + lambert = OrenNayar(GBuffer.normal, light.direction, normalize(GBuffer.position), mod(g_Time, 1.0)); vec4 DiffuseColor = vec4(0, 0, 0, 0); vec4 SpecularColor = vec4(0, 0, 0, 0); - float Roughness = 1.0; + float specPower = pow(2.0, (10.0 * (Roughness / 255.0) + 1.0)); @@ -34,7 +67,7 @@ vec4 ComputeLighting(Light light) { #ifdef SPECULAR vec3 vsCameraDir = normalize(GBuffer.position); vec3 reflection = reflect(light.direction, GBuffer.normal); - float specular = max(dot(normalize(vsCameraDir), normalize(reflection)), 0.0); + float specular = max(dot(vsCameraDir, normalize(reflection)), 0.0); specular = pow(specular, specPower); if (specular > 0.0) { SpecularColor = vec4(1.0) * GBuffer.specular * specular; @@ -44,7 +77,7 @@ vec4 ComputeLighting(Light light) { #endif - return DiffuseColor + SpecularColor*0.0; + return DiffuseColor + SpecularColor * 0.0; } // Other uses.. later on diff --git a/src/me/merciless/dmonkey3/LightRenderer.java b/src/me/merciless/dmonkey3/LightRenderer.java index e96a232..7ff97d6 100644 --- a/src/me/merciless/dmonkey3/LightRenderer.java +++ b/src/me/merciless/dmonkey3/LightRenderer.java @@ -30,19 +30,19 @@ public class LightRenderer implements GBufferListener, SceneProcessor { private Material material; private boolean gbufferChanged = false; private Camera camera; - private Vector3f vsLightDir = new Vector3f(); - private DirectionalLight dl = new DirectionalLight(); private LightRenderer(AssetManager assets) { this.fullscreenQuad = new Geometry("LightRenderQuad", new Quad(1, 1)); material = new Material(assets, "DMonkey/AmbientLight.j3md"); material.setColor("Color", dl.getColor()); - dl.setColor(new ColorRGBA(CSSColor.LightSkyBlue).interpolateLocal(ColorRGBA.White, 0.5f)); + + + dl.setColor(ColorRGBA.White.clone()); fullscreenQuad.setMaterial(material); fullscreenQuad.setCullHint(Spatial.CullHint.Never); - + dl.setDirection(Vector3f.UNIT_XYZ); } @@ -96,11 +96,11 @@ public void postQueue(RenderQueue rq) { public void postFrame(FrameBuffer out) { updateMaterial(); vsLightDir.set(camera.getViewMatrix().mult(dl.getDirection().normalizeLocal()).normalize()); - + Vector3f vec3 = dl.getDirection(); Vector4f vec4 = new Vector4f(vec3.x, vec3.y, vec3.z, 0); camera.getViewMatrix().mult(vec4, vec4); - + vec3 = new Vector3f(vec4.x, vec4.y, vec4.z); material.setVector3("ViewLightDir", vec3); renderManager.getRenderer().setFrameBuffer(null); diff --git a/src/me/merciless/dmonkey3/TestCase.java b/src/me/merciless/dmonkey3/TestCase.java index 9581b60..2d028c7 100644 --- a/src/me/merciless/dmonkey3/TestCase.java +++ b/src/me/merciless/dmonkey3/TestCase.java @@ -8,6 +8,7 @@ import com.jme3.light.DirectionalLight; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; +import com.jme3.math.FastMath; import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; @@ -29,6 +30,7 @@ public class TestCase extends SimpleApplication { @Override public void simpleInitApp() { stateManager.attach(deferred = new DeferredRenderState()); + flyCam.setMoveSpeed(10); material = assetManager.loadMaterial("TestLightMaterial.j3m"); Geometry geom = new Geometry("BoxNode", new Box(1, 1, 1));