From 4bb305e0465074bf0fc8d7788e14da87f8e3b9a4 Mon Sep 17 00:00:00 2001 From: Edward Peterson Date: Sun, 6 Nov 2016 14:52:14 -0500 Subject: [PATCH] Lighting Added Colored Lighting Fixed bug where last light added to map was not lit because of disabled lights --- core/assets/Fragment.frag | 22 +++++++++++++----- core/assets/env.png | Bin 6872 -> 6872 bytes core/assets/testmap.tmx | 14 ++++++----- core/src/com/toasted/chuck/ChuckGame.java | 2 +- core/src/com/toasted/chuck/Graphics.java | 18 ++++++++------ core/src/com/toasted/chuck/Level.java | 6 +++++ core/src/com/toasted/chuck/Light.java | 16 +++++++++++++ .../com/toasted/chuck/entities/Entity.java | 1 + .../com/toasted/chuck/entities/EntityBox.java | 4 +--- .../toasted/chuck/entities/EntityPlayer.java | 8 +++++++ .../toasted/chuck/entities/EntityTorch.java | 1 + 11 files changed, 69 insertions(+), 23 deletions(-) diff --git a/core/assets/Fragment.frag b/core/assets/Fragment.frag index 4cc1b04..64c9ed3 100644 --- a/core/assets/Fragment.frag +++ b/core/assets/Fragment.frag @@ -1,10 +1,12 @@ #version 410 core #define M_PI 3.1415926535897932384626433832795 +#define M_LIGHTLIM 50 //SpriteBatch will use texture unit 0 uniform sampler2D u_texture; uniform vec2 u_screenResolution; -uniform vec2 u_lightCoord[50]; -uniform float u_lightIntensity[50]; +uniform vec2 u_lightCoord[M_LIGHTLIM]; +uniform float u_lightIntensity[M_LIGHTLIM]; +uniform vec3 u_lightColor[M_LIGHTLIM]; uniform int u_actualLights; uniform float u_ambientLight; @@ -32,18 +34,26 @@ void main() { //Distance is measured in pixels - + vec3 finalTint = vec3(1,1,1); float brightest = u_ambientLight; for(int i = 0;i < u_actualLights;i++){ float dis2Light = getDistance(gl_FragCoord.x, gl_FragCoord.y, u_lightCoord[i].x, u_lightCoord[i].y); float dis2Light2 = (1-(min(dis2Light/(disFull * u_lightIntensity[i]), 1))); brightest = brightest + dis2Light2 - brightest * dis2Light2; + finalTint.rgb = finalTint.rgb + (u_lightColor[i].rgb - finalTint.rgb) * dis2Light2; } - texColor.rgb = texColor.rgb * (sin(pow(brightest, 10) * M_PI / 2)); + + + + + //Apply Light Levels + texColor.rgb = texColor.rgb * (sin(pow(brightest, 10) * M_PI / 2)); + //apply torchlight tint - texColor.b = texColor.b * 0.7; - texColor.g = texColor.g * 0.9; + + texColor.rgb = texColor.rgb * finalTint.rgb; + //final color gl_FragColor = texColor * vColor; } diff --git a/core/assets/env.png b/core/assets/env.png index 6edb7efb1f6df42b4108307b02d01013d0d16099..a2c1228612ee0a9324bf3f6b15cb0f8f7a29a6f2 100644 GIT binary patch delta 18 acmca%dc$-=2s;ap)(@SJFE>WlN&x^$2nXQ+ delta 18 Zcmca%dc$-=2s^Wx`m$=*6&s^#r2s|J2NwVU diff --git a/core/assets/testmap.tmx b/core/assets/testmap.tmx index 3df5062..0bd8559 100644 --- a/core/assets/testmap.tmx +++ b/core/assets/testmap.tmx @@ -1,5 +1,5 @@ - + @@ -392,11 +392,7 @@ - - - - - + @@ -565,5 +561,11 @@ + + + + + + diff --git a/core/src/com/toasted/chuck/ChuckGame.java b/core/src/com/toasted/chuck/ChuckGame.java index 35f0d32..ea9a4ba 100644 --- a/core/src/com/toasted/chuck/ChuckGame.java +++ b/core/src/com/toasted/chuck/ChuckGame.java @@ -29,7 +29,7 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); level.draw(graphics); - System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); +// System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); } diff --git a/core/src/com/toasted/chuck/Graphics.java b/core/src/com/toasted/chuck/Graphics.java index 1182a60..9af5e8a 100644 --- a/core/src/com/toasted/chuck/Graphics.java +++ b/core/src/com/toasted/chuck/Graphics.java @@ -82,18 +82,22 @@ public class Graphics { int loc = shader.getUniformLocation("u_lightCoord[0]"); int locIn = shader.getUniformLocation("u_lightIntensity[0]"); - int nullLights = 0; - for(int i = 0;i < lights.size();i++){ - if(lights.get(i) == null || !lights.get(i).isEmitting()){ - nullLights++; + int locCol = shader.getUniformLocation("u_lightColor[0]"); + int i = 0; + for(Light l: lights){ + if(l == null || !l.isEmitting()){ continue; } - Vector3 v3 = cam.project(new Vector3(lights.get(i).getX(), lights.get(i).getY(), 0)); + Vector3 v3 = cam.project(new Vector3(l.getX(), l.getY(), 0)); Vector2 v = new Vector2(v3.x, v3.y); shader.setUniformf(loc + i, v); - shader.setUniformf(locIn + i, lights.get(i).getIntensity() * lightValueMultiplier); + shader.setUniformf(locIn + i, l.getIntensity() * lightValueMultiplier); + + shader.setUniformf(locCol + i, l.getLightColor()); + i++; + } - shader.setUniformi("u_actualLights", lights.size() - nullLights); + shader.setUniformi("u_actualLights", i); shader.setUniformf("u_ambientLight", Light.VAL_AMBIENT); } } diff --git a/core/src/com/toasted/chuck/Level.java b/core/src/com/toasted/chuck/Level.java index 25b9b2c..e2cdb86 100644 --- a/core/src/com/toasted/chuck/Level.java +++ b/core/src/com/toasted/chuck/Level.java @@ -137,6 +137,12 @@ public class Level { String type = (String) o.getProperties().get("type"); if(type.equals("torch")){ newEntity = new EntityTorch(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2); + if(o.getProperties().containsKey("customColor")){ + LightEmitter le = (LightEmitter)newEntity; + String[] rgbValues = ((String)o.getProperties().get("customColor")).split(","); + System.out.println("Setting CUSTOM RGB"); + le.getLight().setLightColor(Float.parseFloat(rgbValues[0]), Float.parseFloat(rgbValues[1]), Float.parseFloat(rgbValues[2])); + } } if(type.equals("box")){ newEntity = new EntityBox(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2); diff --git a/core/src/com/toasted/chuck/Light.java b/core/src/com/toasted/chuck/Light.java index 9b6f17d..1dc5d7b 100644 --- a/core/src/com/toasted/chuck/Light.java +++ b/core/src/com/toasted/chuck/Light.java @@ -1,6 +1,7 @@ package com.toasted.chuck; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; public class Light { public final static float VAL_TORCH = .4f; @@ -10,6 +11,7 @@ public class Light { private Vector2 position; private float intensity = 1; private boolean isEmitting = true; + private Vector3 lightColor = new Vector3(1,1,1); public Light(float x, float y, float intensity){ position = new Vector2(x, y); this.intensity = intensity; @@ -38,4 +40,18 @@ public class Light { public void setEmitting(boolean state){ isEmitting = state; } + public void setLightColor(float r, float g, float b){ + lightColor.set(r, g, b); + } + public Vector3 getLightColor(){ + return lightColor; + } + public String toString(){ + String txt = "X: " + getX(); + txt += "\nY: " + getY(); + txt += "\nIntensity: " + getIntensity(); + txt += "\nIsEmitting: " + isEmitting(); + txt += "\nLightColor: " + getLightColor(); + return txt; + } } diff --git a/core/src/com/toasted/chuck/entities/Entity.java b/core/src/com/toasted/chuck/entities/Entity.java index 5fb76fd..a176dfa 100644 --- a/core/src/com/toasted/chuck/entities/Entity.java +++ b/core/src/com/toasted/chuck/entities/Entity.java @@ -19,6 +19,7 @@ public abstract class Entity { protected boolean isHostile = false; protected float stunTimer = 0; protected float weight = 0; //used for destruction capabilities and pressure plate triggering + protected int health = 3; public Entity(){ } diff --git a/core/src/com/toasted/chuck/entities/EntityBox.java b/core/src/com/toasted/chuck/entities/EntityBox.java index d9b9130..8bf8921 100644 --- a/core/src/com/toasted/chuck/entities/EntityBox.java +++ b/core/src/com/toasted/chuck/entities/EntityBox.java @@ -31,7 +31,7 @@ public class EntityBox extends Entity{ if(flyLength > 0){ //can hit enemies while in air for(Entity e: lvl.getEntities()){ - if(e.isHostile && e.collision.overlaps(collision)){ + if(e.isHostile && e.collision.overlaps(collision) && weight > .75f){ velocity.x = 0; velocity.y = 0; e.stun(4f); @@ -41,8 +41,6 @@ public class EntityBox extends Entity{ } public void draw(Graphics g) { -// g.getShapes().setColor(Color.BLUE); -// g.getShapes().rect(position.x, position.y + (float)Math.sin((1 - Math.max(flyLength, 0) / .5f) * Math.PI) * 8, 16, 16); if(shouldDrawSelf) draw(g, 0, 0); diff --git a/core/src/com/toasted/chuck/entities/EntityPlayer.java b/core/src/com/toasted/chuck/entities/EntityPlayer.java index 62dbbe6..1f811c0 100644 --- a/core/src/com/toasted/chuck/entities/EntityPlayer.java +++ b/core/src/com/toasted/chuck/entities/EntityPlayer.java @@ -47,6 +47,14 @@ public class EntityPlayer extends Entity implements LightEmitter{ if(e.collision.overlaps(collision) && e.isChuckable){ holding = e; holding.shouldDrawSelf = false; + + if(holding instanceof LightEmitter){ + LightEmitter le = (LightEmitter)holding; + System.err.println(le.getLight()); + } + + + break; } } diff --git a/core/src/com/toasted/chuck/entities/EntityTorch.java b/core/src/com/toasted/chuck/entities/EntityTorch.java index 875fa4c..35e84fe 100644 --- a/core/src/com/toasted/chuck/entities/EntityTorch.java +++ b/core/src/com/toasted/chuck/entities/EntityTorch.java @@ -28,6 +28,7 @@ public class EntityTorch extends EntityBox implements LightEmitter{ public EntityTorch(float x, float y) { super(x, y); emitter = new Light(x + collision.width / 2, y + collision.height / 2, .3f); + emitter.setLightColor(1, .9f, .7f); weight = 0; }