Skip to content

Commit e98c2fe

Browse files
committed
Light shafts
1 parent dd1f391 commit e98c2fe

File tree

17 files changed

+276
-69
lines changed

17 files changed

+276
-69
lines changed

core/assets/graphics/postprocessing/default.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22
buffers: [
33
{ width: 512, height: 512, format: "RGB888", filter: "Linear", wrap: "MirroredRepeat", depth: false, name: "my:small-color" },
44
{ width: 512, height: 512, format: "RGB888", filter: "Linear", wrap: "ClampToEdge", depth: false, name: "my:blur" }
5+
{ width: 512, height: 512, format: "RGBA8888", filter: "Linear", wrap: "ClampToEdge", depth: false, name: "my:light-scattering" }
56
],
67

78
steps: [
89
{ copy: "forge:main-color", target: "my:small-color" },
10+
{
11+
target: "my:light-scattering",
12+
fragment: "fix-light-scattering",
13+
customUniforms: {
14+
u_mainTexture: "forge:light-scattering",
15+
u_density: 0.80,
16+
u_weight: 5.65,
17+
u_exposure: 0.0034,
18+
u_decay: 1.0,
19+
u_numSamples: 50
20+
},
21+
uniforms: [
22+
"LightPositionOnScreen"
23+
]
24+
},
925
{
1026
target: "my:blur",
1127
fragment: "blur",
@@ -23,6 +39,7 @@
2339
customUniforms: {
2440
u_mainTexture: "forge:main-color",
2541
u_blurTexture: "my:blur",
42+
u_lightScatteringTexture: "my:light-scattering",
2643
u_blurMix: 0.25
2744
}
2845
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
varying vec2 v_texCoords;
22
void main() {
3-
vec4 mainColor = texture2D(u_mainTexture, v_texCoords);
4-
vec4 vignetteColor = texture2D(u_vignetteTexture, v_texCoords);
5-
vec4 blurColor = texture2D(u_blurTexture, v_texCoords);
6-
gl_FragColor = mix(mainColor, blurColor, u_blurMix) * vignetteColor;
3+
vec4 mainColor = texture2D(u_mainTexture, v_texCoords);
4+
vec4 vignetteColor = texture2D(u_vignetteTexture, v_texCoords);
5+
vec4 blurColor = texture2D(u_blurTexture, v_texCoords);
6+
vec4 lightScatteringColor = texture2D(u_lightScatteringTexture, v_texCoords);
7+
gl_FragColor = (mix(mainColor, blurColor, u_blurMix) + lightScatteringColor ) * vignetteColor;
8+
// gl_FragColor = mix(mainColor, lightScatteringColor, 0.8f);
79
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
varying vec2 v_texCoords;
3+
4+
vec4 getSampleColor(vec2 coords) {
5+
vec4 rawColor = texture2D(u_mainTexture, coords);
6+
7+
if (rawColor.r >= 0.01f && rawColor.g == 0.0f && rawColor.b == 0.0f) {
8+
return vec4(0.0f, 0.0f, 0.0f, 1.0f);
9+
} else {
10+
return rawColor;
11+
}
12+
}
13+
14+
void main() {
15+
vec4 finalColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
16+
17+
if (u_lightDirDotViewDir < 0.0f) {
18+
float lightDirDotViewDir = abs(u_lightDirDotViewDir);
19+
vec2 texCoord = v_texCoords.st;
20+
vec2 deltaTexCoord = (1.0 / float(u_numSamples)) * u_density * vec2(texCoord.xy - u_lightPositonOnScreen.xy);
21+
float dist = length(deltaTexCoord.xy);
22+
23+
float threshold = 0.01f;
24+
if (dist > threshold) {
25+
deltaTexCoord.xy /= dist / threshold;
26+
}
27+
28+
float illuminationDecay = 1.0f;
29+
for(int i=0; i < int(u_numSamples); i++) {
30+
texCoord -= deltaTexCoord;
31+
vec4 sampleColor = getSampleColor(texCoord);
32+
sampleColor *= illuminationDecay * u_weight;
33+
finalColor += sampleColor;
34+
illuminationDecay *= u_decay;
35+
}
36+
37+
finalColor *= u_exposure * lightDirDotViewDir;
38+
}
39+
40+
gl_FragColor = finalColor;
41+
}

core/assets/graphics/shaders/helpers/shadow_map.glsl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ float calculateDepth(vec4 position, vec4 eyePosition, float cameraFar) {
33
}
44

55
vec4 pack(float depth) {
6-
const vec4 bias = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);
7-
vec4 color = vec4(depth, fract(depth * 255.0), fract(depth * 65025.0), fract(depth * 160581375.0));
8-
return color - (color.yzww * bias);
6+
//const vec4 bias = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);
7+
//vec4 color = vec4(depth, fract(depth * 255.0), fract(depth * 65025.0), fract(depth * 160581375.0));
8+
// return color - (color.yzww * bias);
9+
return vec4(depth, 0.0f, 0.0f, 1.0f);
910
}
1011

1112
float unpack(vec4 packedZValue) {
12-
const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0);
13-
return dot(packedZValue,bitShifts);
13+
//const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0);
14+
//return dot(packedZValue,bitShifts);
15+
return packedZValue.r;
1416
}
1517

1618
float shadowBias(vec3 normal, vec3 lightDir) {

core/src/macbury/forge/Config.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class Config extends KVStorage<Config.Key> {
1616
public enum Key {
1717
ResolutionWidth, ResolutionHeight, Fullscreen, Debug, NearShadowMapSize, FarShadowMapSize, BloomTextureSize, ReflectionBufferSize, RefractionBufferSize,
18-
GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor,
18+
GenerateWireframe, NearShadowDistance, RenderDebug, RenderDynamicOctree, RenderStaticOctree, RenderBoundingBox, RenderBulletDebug, Editor, LightScatteringTextureSize
1919
}
2020

2121
public enum RenderDebug {
@@ -33,6 +33,7 @@ public void setDefaults() {
3333
putInt(Key.BloomTextureSize, 512);
3434
putInt(Key.ReflectionBufferSize, 512);
3535
putInt(Key.RefractionBufferSize, 512);
36+
putInt(Key.LightScatteringTextureSize, 512);
3637
putInt(Key.NearShadowDistance, 10);
3738
putBool(Key.Fullscreen, false);
3839
putBool(Key.Debug, false);

core/src/macbury/forge/graphics/fbo/Fbo.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
* Created by macbury on 20.07.15.
55
*/
66
public class Fbo {
7-
public final static String FRAMEBUFFER_MAIN_COLOR = "forge:main-color";
8-
public final static String FRAMEBUFFER_SUN_FAR_DEPTH = "forge:sun-far";
9-
public final static String FRAMEBUFFER_SUN_NEAR_DEPTH = "forge:sun-near";
7+
public final static String FRAMEBUFFER_SUN_FAR_DEPTH = "forge:sun-far";
8+
public final static String FRAMEBUFFER_SUN_NEAR_DEPTH = "forge:sun-near";
109

11-
public static final String FRAMEBUFFER_REFLECTIONS = "forge:reflections";
12-
public static final String FRAMEBUFFER_REFRACTIONS = "forge:refractions";
13-
public static final String FRAMEBUFFER_FINAL = "forge:final-color";
10+
public static final String FRAMEBUFFER_REFLECTIONS = "forge:reflections";
11+
public static final String FRAMEBUFFER_REFRACTIONS = "forge:refractions";
12+
13+
public static final String FRAMEBUFFER_LIGHT_SCATTERING = "forge:light-scattering";
14+
15+
public final static String FRAMEBUFFER_MAIN_COLOR = "forge:main-color";
16+
public static final String FRAMEBUFFER_FINAL = "forge:final-color";
1417
}

core/src/macbury/forge/graphics/fbo/FrameBufferManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void begin(String fbIdn) {
190190
public void createDefaultFrameBuffers() {
191191
create(Fbo.FRAMEBUFFER_FINAL);
192192
create(Fbo.FRAMEBUFFER_MAIN_COLOR);
193-
193+
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);
194194
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);
195195
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);
196196
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);

core/src/macbury/forge/graphics/skybox/DayNightSkybox.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class DayNightSkybox extends Skybox {
5050
private Color fogColor = new Color();
5151
private Color ambientColor = new Color();
5252
private Color sunColor = new Color();
53+
private boolean renderOnlySun;
54+
5355
@Override
5456
public void update(float delta, Camera camera) {
5557
updateFogColor();
@@ -132,7 +134,8 @@ public void dispose() {
132134
@Override
133135
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
134136
//renderables.add(buildSunMoon());
135-
renderables.add(getCubeRenderable());
137+
if (!renderOnlySun)
138+
renderables.add(getCubeRenderable());
136139

137140
}
138141

@@ -276,4 +279,15 @@ public Texture getSkyMapTexture() {
276279
return skyMapTexture;
277280
}
278281

282+
public void setRenderOnlySun(boolean renderOnlySun) {
283+
this.renderOnlySun = renderOnlySun;
284+
}
285+
286+
public boolean isRenderOnlySun() {
287+
return renderOnlySun;
288+
}
289+
290+
public void getLightPosition(Vector3 out) {
291+
sunMonRenderable.worldTransform.getTranslation(out);
292+
}
279293
}

core/src/macbury/forge/level/Level.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Level(LevelState state, TerrainGeometryProvider geometryProvider) {
6767
this.octree = OctreeNode.root();
6868

6969
this.batch = new VoxelBatch(renderContext, colorShaderProvider);
70-
this.camera = new GameCamera();
70+
this.camera = env.mainCamera;
7171
this.frustrumDebugger = new FrustrumDebugAndRenderer();
7272
frustrumDebugger.add(camera);
7373
frustrumDebugger.add(env.mainLight.getShadowCamera());

core/src/macbury/forge/level/env/LevelEnv.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.badlogic.gdx.math.Vector3;
99
import com.badlogic.gdx.utils.Disposable;
1010
import macbury.forge.assets.assets.TextureAsset;
11+
import macbury.forge.graphics.camera.GameCamera;
1112
import macbury.forge.graphics.light.OrthographicDirectionalLight;
1213
import macbury.forge.graphics.skybox.CubemapSkybox;
1314
import macbury.forge.graphics.skybox.Skybox;
@@ -17,10 +18,16 @@
1718
* Created by macbury on 28.10.14.
1819
*/
1920
public class LevelEnv implements Disposable {
21+
public GameCamera mainCamera;
2022

2123
public enum ClipMode {
2224
None, Reflection, Refraction
2325
}
26+
27+
public enum DepthShaderMode {
28+
Normal, OnlyFront
29+
}
30+
2431
public Skybox skybox;
2532
public OrthographicDirectionalLight mainLight;
2633
public Color ambientLight;
@@ -33,9 +40,11 @@ public enum ClipMode {
3340
public Vector3 gravity = new Vector3(0, -6f, 0);
3441
private Texture windDisplacementTexture;
3542

43+
public DepthShaderMode depthShaderMode = DepthShaderMode.Normal;
3644
public WaterEnv water;
3745

3846
public LevelEnv() {
47+
mainCamera = new GameCamera();
3948
water = new WaterEnv();
4049
skyColor = Color.valueOf("3498db");
4150
fogColor = new Color(skyColor);

core/src/macbury/forge/shaders/DepthShader.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.badlogic.gdx.graphics.GL30;
55
import com.badlogic.gdx.graphics.g3d.Renderable;
66
import macbury.forge.graphics.batch.renderable.BaseRenderable;
7+
import macbury.forge.level.env.LevelEnv;
78
import macbury.forge.shaders.utils.RenderableBaseShader;
89

910
/**
@@ -19,11 +20,16 @@ public boolean canRender(Renderable instance) {
1920
public void beforeRender(Renderable renderable) {
2021
context.setDepthMask(true);
2122
context.setDepthTest(GL30.GL_LESS);
22-
if (BaseRenderable.haveTransparency(renderable.material)) {
23-
context.setCullFace(GL30.GL_NONE);
23+
if (env.depthShaderMode == LevelEnv.DepthShaderMode.Normal) {
24+
if (BaseRenderable.haveTransparency(renderable.material)) {
25+
context.setCullFace(GL30.GL_NONE);
26+
} else {
27+
context.setCullFace(GL20.GL_FRONT);
28+
}
2429
} else {
25-
context.setCullFace(GL20.GL_FRONT);
30+
context.setCullFace(GL20.GL_BACK);
2631
}
32+
2733
}
2834

2935
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package macbury.forge.shaders.uniforms;
2+
3+
import com.badlogic.gdx.graphics.Camera;
4+
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
5+
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
6+
import com.badlogic.gdx.math.Vector2;
7+
import com.badlogic.gdx.math.Vector3;
8+
import macbury.forge.ForgE;
9+
import macbury.forge.graphics.skybox.DayNightSkybox;
10+
import macbury.forge.level.env.LevelEnv;
11+
import macbury.forge.shaders.utils.BaseUniform;
12+
13+
/**
14+
* Created by macbury on 31.08.15.
15+
*/
16+
public class UniformLightPositionOnScreen extends BaseUniform {
17+
private static final String UNIFORM_POS_ON_SCREEN = "u_lightPositonOnScreen";
18+
private static final String UNIFORM_DOT_DIR_VIEW_NAME = "u_lightDirDotViewDir";
19+
20+
private static final float OFF_SCREEN_RENDER_RATIO = 2.0F;
21+
private final static Vector3 tempVec = new Vector3();
22+
@Override
23+
public void defineUniforms() {
24+
define(UNIFORM_POS_ON_SCREEN, Vector2.class);
25+
define(UNIFORM_DOT_DIR_VIEW_NAME, Float.class);
26+
}
27+
28+
@Override
29+
public void bind(ShaderProgram shader, LevelEnv env, RenderContext context, Camera camera) {
30+
if (ForgE.time.isDay()) {
31+
shader.setUniformf(
32+
UNIFORM_DOT_DIR_VIEW_NAME,
33+
tempVec.set(env.mainLight.direction).dot(env.mainCamera.direction)
34+
);
35+
} else {
36+
shader.setUniformf( UNIFORM_DOT_DIR_VIEW_NAME, 1 );
37+
}
38+
39+
40+
if (DayNightSkybox.class.isInstance(env.skybox)) {
41+
DayNightSkybox dayNightSkybox = (DayNightSkybox)env.skybox;
42+
dayNightSkybox.getLightPosition(tempVec);
43+
env.mainCamera.project(tempVec);
44+
45+
/*tempVec.x /= env.mainCamera.far;
46+
tempVec.y /= env.mainCamera.far;
47+
48+
tempVec.x = (tempVec.x + 1.0f) / 2.0f;
49+
tempVec.y = (tempVec.y + 1.0f) / 2.0f;
50+
//tempVec.z /= camera.far;
51+
*/
52+
shader.setUniformf(
53+
UNIFORM_POS_ON_SCREEN,
54+
tempVec.x / env.mainCamera.viewportWidth,
55+
tempVec.y / env.mainCamera.viewportHeight
56+
);
57+
}
58+
}
59+
60+
@Override
61+
public void dispose() {
62+
63+
}
64+
}

0 commit comments

Comments
 (0)