Skip to content

Commit

Permalink
Light shafts
Browse files Browse the repository at this point in the history
  • Loading branch information
macbury committed Aug 31, 2015
1 parent dd1f391 commit e98c2fe
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 69 deletions.
17 changes: 17 additions & 0 deletions core/assets/graphics/postprocessing/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@
buffers: [
{ width: 512, height: 512, format: "RGB888", filter: "Linear", wrap: "MirroredRepeat", depth: false, name: "my:small-color" },
{ width: 512, height: 512, format: "RGB888", filter: "Linear", wrap: "ClampToEdge", depth: false, name: "my:blur" }
{ width: 512, height: 512, format: "RGBA8888", filter: "Linear", wrap: "ClampToEdge", depth: false, name: "my:light-scattering" }
],

steps: [
{ copy: "forge:main-color", target: "my:small-color" },
{
target: "my:light-scattering",
fragment: "fix-light-scattering",
customUniforms: {
u_mainTexture: "forge:light-scattering",
u_density: 0.80,
u_weight: 5.65,
u_exposure: 0.0034,
u_decay: 1.0,
u_numSamples: 50
},
uniforms: [
"LightPositionOnScreen"
]
},
{
target: "my:blur",
fragment: "blur",
Expand All @@ -23,6 +39,7 @@
customUniforms: {
u_mainTexture: "forge:main-color",
u_blurTexture: "my:blur",
u_lightScatteringTexture: "my:light-scattering",
u_blurMix: 0.25
}
}
Expand Down
10 changes: 6 additions & 4 deletions core/assets/graphics/postprocessing/final.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
varying vec2 v_texCoords;
void main() {
vec4 mainColor = texture2D(u_mainTexture, v_texCoords);
vec4 vignetteColor = texture2D(u_vignetteTexture, v_texCoords);
vec4 blurColor = texture2D(u_blurTexture, v_texCoords);
gl_FragColor = mix(mainColor, blurColor, u_blurMix) * vignetteColor;
vec4 mainColor = texture2D(u_mainTexture, v_texCoords);
vec4 vignetteColor = texture2D(u_vignetteTexture, v_texCoords);
vec4 blurColor = texture2D(u_blurTexture, v_texCoords);
vec4 lightScatteringColor = texture2D(u_lightScatteringTexture, v_texCoords);
gl_FragColor = (mix(mainColor, blurColor, u_blurMix) + lightScatteringColor ) * vignetteColor;
// gl_FragColor = mix(mainColor, lightScatteringColor, 0.8f);
}
41 changes: 41 additions & 0 deletions core/assets/graphics/postprocessing/fix-light-scattering.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

varying vec2 v_texCoords;

vec4 getSampleColor(vec2 coords) {
vec4 rawColor = texture2D(u_mainTexture, coords);

if (rawColor.r >= 0.01f && rawColor.g == 0.0f && rawColor.b == 0.0f) {
return vec4(0.0f, 0.0f, 0.0f, 1.0f);
} else {
return rawColor;
}
}

void main() {
vec4 finalColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);

if (u_lightDirDotViewDir < 0.0f) {
float lightDirDotViewDir = abs(u_lightDirDotViewDir);
vec2 texCoord = v_texCoords.st;
vec2 deltaTexCoord = (1.0 / float(u_numSamples)) * u_density * vec2(texCoord.xy - u_lightPositonOnScreen.xy);
float dist = length(deltaTexCoord.xy);

float threshold = 0.01f;
if (dist > threshold) {
deltaTexCoord.xy /= dist / threshold;
}

float illuminationDecay = 1.0f;
for(int i=0; i < int(u_numSamples); i++) {
texCoord -= deltaTexCoord;
vec4 sampleColor = getSampleColor(texCoord);
sampleColor *= illuminationDecay * u_weight;
finalColor += sampleColor;
illuminationDecay *= u_decay;
}

finalColor *= u_exposure * lightDirDotViewDir;
}

gl_FragColor = finalColor;
}
12 changes: 7 additions & 5 deletions core/assets/graphics/shaders/helpers/shadow_map.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ float calculateDepth(vec4 position, vec4 eyePosition, float cameraFar) {
}

vec4 pack(float depth) {
const vec4 bias = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);
vec4 color = vec4(depth, fract(depth * 255.0), fract(depth * 65025.0), fract(depth * 160581375.0));
return color - (color.yzww * bias);
//const vec4 bias = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);
//vec4 color = vec4(depth, fract(depth * 255.0), fract(depth * 65025.0), fract(depth * 160581375.0));
// return color - (color.yzww * bias);
return vec4(depth, 0.0f, 0.0f, 1.0f);
}

float unpack(vec4 packedZValue) {
const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0);
return dot(packedZValue,bitShifts);
//const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0);
//return dot(packedZValue,bitShifts);
return packedZValue.r;
}

float shadowBias(vec3 normal, vec3 lightDir) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/macbury/forge/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class Config extends KVStorage<Config.Key> {
public enum Key {
ResolutionWidth, ResolutionHeight, Fullscreen, Debug, NearShadowMapSize, FarShadowMapSize, BloomTextureSize, ReflectionBufferSize, RefractionBufferSize,
GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor,
GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor, LightScatteringTextureSize
}

public enum RenderDebug {
Expand All @@ -33,6 +33,7 @@ public void setDefaults() {
putInt(Key.BloomTextureSize, 512);
putInt(Key.ReflectionBufferSize, 512);
putInt(Key.RefractionBufferSize, 512);
putInt(Key.LightScatteringTextureSize, 512);
putInt(Key.NearShadowDistance, 10);
putBool(Key.Fullscreen, false);
putBool(Key.Debug, false);
Expand Down
15 changes: 9 additions & 6 deletions core/src/macbury/forge/graphics/fbo/Fbo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* Created by macbury on 20.07.15.
*/
public class Fbo {
public final static String FRAMEBUFFER_MAIN_COLOR = "forge:main-color";
public final static String FRAMEBUFFER_SUN_FAR_DEPTH = "forge:sun-far";
public final static String FRAMEBUFFER_SUN_NEAR_DEPTH = "forge:sun-near";
public final static String FRAMEBUFFER_SUN_FAR_DEPTH = "forge:sun-far";
public final static String FRAMEBUFFER_SUN_NEAR_DEPTH = "forge:sun-near";

public static final String FRAMEBUFFER_REFLECTIONS = "forge:reflections";
public static final String FRAMEBUFFER_REFRACTIONS = "forge:refractions";
public static final String FRAMEBUFFER_FINAL = "forge:final-color";
public static final String FRAMEBUFFER_REFLECTIONS = "forge:reflections";
public static final String FRAMEBUFFER_REFRACTIONS = "forge:refractions";

public static final String FRAMEBUFFER_LIGHT_SCATTERING = "forge:light-scattering";

public final static String FRAMEBUFFER_MAIN_COLOR = "forge:main-color";
public static final String FRAMEBUFFER_FINAL = "forge:final-color";
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void begin(String fbIdn) {
public void createDefaultFrameBuffers() {
create(Fbo.FRAMEBUFFER_FINAL);
create(Fbo.FRAMEBUFFER_MAIN_COLOR);

create(Fbo.FRAMEBUFFER_LIGHT_SCATTERING, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.LightScatteringTextureSize), ForgE.config.getInt(Config.Key.LightScatteringTextureSize), true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Nearest);
create(Fbo.FRAMEBUFFER_REFLECTIONS, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.ReflectionBufferSize), ForgE.config.getInt(Config.Key.ReflectionBufferSize), true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear);
create(Fbo.FRAMEBUFFER_REFRACTIONS, Pixmap.Format.RGBA8888, ForgE.config.getInt(Config.Key.RefractionBufferSize), ForgE.config.getInt(Config.Key.RefractionBufferSize), true, Texture.TextureWrap.Repeat, Texture.TextureFilter.Linear);
createFloat(Fbo.FRAMEBUFFER_SUN_FAR_DEPTH, ForgE.config.getInt(Config.Key.FarShadowMapSize), ForgE.config.getInt(Config.Key.FarShadowMapSize), true, Texture.TextureWrap.ClampToEdge, Texture.TextureFilter.Nearest);
Expand Down
16 changes: 15 additions & 1 deletion core/src/macbury/forge/graphics/skybox/DayNightSkybox.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class DayNightSkybox extends Skybox {
private Color fogColor = new Color();
private Color ambientColor = new Color();
private Color sunColor = new Color();
private boolean renderOnlySun;

@Override
public void update(float delta, Camera camera) {
updateFogColor();
Expand Down Expand Up @@ -132,7 +134,8 @@ public void dispose() {
@Override
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
//renderables.add(buildSunMoon());
renderables.add(getCubeRenderable());
if (!renderOnlySun)
renderables.add(getCubeRenderable());

}

Expand Down Expand Up @@ -276,4 +279,15 @@ public Texture getSkyMapTexture() {
return skyMapTexture;
}

public void setRenderOnlySun(boolean renderOnlySun) {
this.renderOnlySun = renderOnlySun;
}

public boolean isRenderOnlySun() {
return renderOnlySun;
}

public void getLightPosition(Vector3 out) {
sunMonRenderable.worldTransform.getTranslation(out);
}
}
2 changes: 1 addition & 1 deletion core/src/macbury/forge/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Level(LevelState state, TerrainGeometryProvider geometryProvider) {
this.octree = OctreeNode.root();

this.batch = new VoxelBatch(renderContext, colorShaderProvider);
this.camera = new GameCamera();
this.camera = env.mainCamera;
this.frustrumDebugger = new FrustrumDebugAndRenderer();
frustrumDebugger.add(camera);
frustrumDebugger.add(env.mainLight.getShadowCamera());
Expand Down
9 changes: 9 additions & 0 deletions core/src/macbury/forge/level/env/LevelEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Disposable;
import macbury.forge.assets.assets.TextureAsset;
import macbury.forge.graphics.camera.GameCamera;
import macbury.forge.graphics.light.OrthographicDirectionalLight;
import macbury.forge.graphics.skybox.CubemapSkybox;
import macbury.forge.graphics.skybox.Skybox;
Expand All @@ -17,10 +18,16 @@
* Created by macbury on 28.10.14.
*/
public class LevelEnv implements Disposable {
public GameCamera mainCamera;

public enum ClipMode {
None, Reflection, Refraction
}

public enum DepthShaderMode {
Normal, OnlyFront
}

public Skybox skybox;
public OrthographicDirectionalLight mainLight;
public Color ambientLight;
Expand All @@ -33,9 +40,11 @@ public enum ClipMode {
public Vector3 gravity = new Vector3(0, -6f, 0);
private Texture windDisplacementTexture;

public DepthShaderMode depthShaderMode = DepthShaderMode.Normal;
public WaterEnv water;

public LevelEnv() {
mainCamera = new GameCamera();
water = new WaterEnv();
skyColor = Color.valueOf("3498db");
fogColor = new Color(skyColor);
Expand Down
12 changes: 9 additions & 3 deletions core/src/macbury/forge/shaders/DepthShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g3d.Renderable;
import macbury.forge.graphics.batch.renderable.BaseRenderable;
import macbury.forge.level.env.LevelEnv;
import macbury.forge.shaders.utils.RenderableBaseShader;

/**
Expand All @@ -19,11 +20,16 @@ public boolean canRender(Renderable instance) {
public void beforeRender(Renderable renderable) {
context.setDepthMask(true);
context.setDepthTest(GL30.GL_LESS);
if (BaseRenderable.haveTransparency(renderable.material)) {
context.setCullFace(GL30.GL_NONE);
if (env.depthShaderMode == LevelEnv.DepthShaderMode.Normal) {
if (BaseRenderable.haveTransparency(renderable.material)) {
context.setCullFace(GL30.GL_NONE);
} else {
context.setCullFace(GL20.GL_FRONT);
}
} else {
context.setCullFace(GL20.GL_FRONT);
context.setCullFace(GL20.GL_BACK);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package macbury.forge.shaders.uniforms;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import macbury.forge.ForgE;
import macbury.forge.graphics.skybox.DayNightSkybox;
import macbury.forge.level.env.LevelEnv;
import macbury.forge.shaders.utils.BaseUniform;

/**
* Created by macbury on 31.08.15.
*/
public class UniformLightPositionOnScreen extends BaseUniform {
private static final String UNIFORM_POS_ON_SCREEN = "u_lightPositonOnScreen";
private static final String UNIFORM_DOT_DIR_VIEW_NAME = "u_lightDirDotViewDir";

private static final float OFF_SCREEN_RENDER_RATIO = 2.0F;
private final static Vector3 tempVec = new Vector3();
@Override
public void defineUniforms() {
define(UNIFORM_POS_ON_SCREEN, Vector2.class);
define(UNIFORM_DOT_DIR_VIEW_NAME, Float.class);
}

@Override
public void bind(ShaderProgram shader, LevelEnv env, RenderContext context, Camera camera) {
if (ForgE.time.isDay()) {
shader.setUniformf(
UNIFORM_DOT_DIR_VIEW_NAME,
tempVec.set(env.mainLight.direction).dot(env.mainCamera.direction)
);
} else {
shader.setUniformf( UNIFORM_DOT_DIR_VIEW_NAME, 1 );
}


if (DayNightSkybox.class.isInstance(env.skybox)) {
DayNightSkybox dayNightSkybox = (DayNightSkybox)env.skybox;
dayNightSkybox.getLightPosition(tempVec);
env.mainCamera.project(tempVec);

/*tempVec.x /= env.mainCamera.far;
tempVec.y /= env.mainCamera.far;
tempVec.x = (tempVec.x + 1.0f) / 2.0f;
tempVec.y = (tempVec.y + 1.0f) / 2.0f;
//tempVec.z /= camera.far;
*/
shader.setUniformf(
UNIFORM_POS_ON_SCREEN,
tempVec.x / env.mainCamera.viewportWidth,
tempVec.y / env.mainCamera.viewportHeight
);
}
}

@Override
public void dispose() {

}
}
Loading

0 comments on commit e98c2fe

Please sign in to comment.