Added Colored Lighting
Fixed bug where last light added to map was not lit because of disabled
lights
master
Edward Peterson 9 years ago
parent 67c2bf37f0
commit 4bb305e046

@ -1,10 +1,12 @@
#version 410 core #version 410 core
#define M_PI 3.1415926535897932384626433832795 #define M_PI 3.1415926535897932384626433832795
#define M_LIGHTLIM 50
//SpriteBatch will use texture unit 0 //SpriteBatch will use texture unit 0
uniform sampler2D u_texture; uniform sampler2D u_texture;
uniform vec2 u_screenResolution; uniform vec2 u_screenResolution;
uniform vec2 u_lightCoord[50]; uniform vec2 u_lightCoord[M_LIGHTLIM];
uniform float u_lightIntensity[50]; uniform float u_lightIntensity[M_LIGHTLIM];
uniform vec3 u_lightColor[M_LIGHTLIM];
uniform int u_actualLights; uniform int u_actualLights;
uniform float u_ambientLight; uniform float u_ambientLight;
@ -32,18 +34,26 @@ void main() {
//Distance is measured in pixels //Distance is measured in pixels
vec3 finalTint = vec3(1,1,1);
float brightest = u_ambientLight; float brightest = u_ambientLight;
for(int i = 0;i < u_actualLights;i++){ 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 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))); float dis2Light2 = (1-(min(dis2Light/(disFull * u_lightIntensity[i]), 1)));
brightest = brightest + dis2Light2 - brightest * dis2Light2; brightest = brightest + dis2Light2 - brightest * dis2Light2;
finalTint.rgb = finalTint.rgb + (u_lightColor[i].rgb - finalTint.rgb) * dis2Light2;
} }
//Apply Light Levels
texColor.rgb = texColor.rgb * (sin(pow(brightest, 10) * M_PI / 2)); texColor.rgb = texColor.rgb * (sin(pow(brightest, 10) * M_PI / 2));
//apply torchlight tint //apply torchlight tint
texColor.b = texColor.b * 0.7;
texColor.g = texColor.g * 0.9; texColor.rgb = texColor.rgb * finalTint.rgb;
//final color //final color
gl_FragColor = texColor * vColor; gl_FragColor = texColor * vColor;
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="50" height="50" tilewidth="16" tileheight="16" nextobjectid="268"> <map version="1.0" orientation="orthogonal" renderorder="right-up" width="50" height="50" tilewidth="16" tileheight="16" nextobjectid="269">
<tileset firstgid="1" name="env" tilewidth="16" tileheight="16" tilecount="64" columns="8"> <tileset firstgid="1" name="env" tilewidth="16" tileheight="16" tilecount="64" columns="8">
<image source="env.png" width="128" height="128"/> <image source="env.png" width="128" height="128"/>
</tileset> </tileset>
@ -392,11 +392,7 @@
</object> </object>
<object id="161" x="720" y="57" width="16" height="16"> <object id="161" x="720" y="57" width="16" height="16">
<properties> <properties>
<property name="type" value="torch"/> <property name="customColor" value="0,1,0"/>
</properties>
</object>
<object id="162" x="768" y="57" width="16" height="16">
<properties>
<property name="type" value="torch"/> <property name="type" value="torch"/>
</properties> </properties>
</object> </object>
@ -565,5 +561,11 @@
<property name="type" value="bat"/> <property name="type" value="bat"/>
</properties> </properties>
</object> </object>
<object id="268" x="768" y="57" width="16" height="16">
<properties>
<property name="customColor" value="0,1,0"/>
<property name="type" value="torch"/>
</properties>
</object>
</objectgroup> </objectgroup>
</map> </map>

@ -29,7 +29,7 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
level.draw(graphics); level.draw(graphics);
System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); // System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond());
} }

@ -82,18 +82,22 @@ public class Graphics {
int loc = shader.getUniformLocation("u_lightCoord[0]"); int loc = shader.getUniformLocation("u_lightCoord[0]");
int locIn = shader.getUniformLocation("u_lightIntensity[0]"); int locIn = shader.getUniformLocation("u_lightIntensity[0]");
int nullLights = 0; int locCol = shader.getUniformLocation("u_lightColor[0]");
for(int i = 0;i < lights.size();i++){ int i = 0;
if(lights.get(i) == null || !lights.get(i).isEmitting()){ for(Light l: lights){
nullLights++; if(l == null || !l.isEmitting()){
continue; 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); Vector2 v = new Vector2(v3.x, v3.y);
shader.setUniformf(loc + i, v); 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); shader.setUniformf("u_ambientLight", Light.VAL_AMBIENT);
} }
} }

@ -137,6 +137,12 @@ public class Level {
String type = (String) o.getProperties().get("type"); String type = (String) o.getProperties().get("type");
if(type.equals("torch")){ if(type.equals("torch")){
newEntity = new EntityTorch(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2); 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")){ if(type.equals("box")){
newEntity = new EntityBox(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2); newEntity = new EntityBox(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2);

@ -1,6 +1,7 @@
package com.toasted.chuck; package com.toasted.chuck;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
public class Light { public class Light {
public final static float VAL_TORCH = .4f; public final static float VAL_TORCH = .4f;
@ -10,6 +11,7 @@ public class Light {
private Vector2 position; private Vector2 position;
private float intensity = 1; private float intensity = 1;
private boolean isEmitting = true; private boolean isEmitting = true;
private Vector3 lightColor = new Vector3(1,1,1);
public Light(float x, float y, float intensity){ public Light(float x, float y, float intensity){
position = new Vector2(x, y); position = new Vector2(x, y);
this.intensity = intensity; this.intensity = intensity;
@ -38,4 +40,18 @@ public class Light {
public void setEmitting(boolean state){ public void setEmitting(boolean state){
isEmitting = 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;
}
} }

@ -19,6 +19,7 @@ public abstract class Entity {
protected boolean isHostile = false; protected boolean isHostile = false;
protected float stunTimer = 0; protected float stunTimer = 0;
protected float weight = 0; //used for destruction capabilities and pressure plate triggering protected float weight = 0; //used for destruction capabilities and pressure plate triggering
protected int health = 3;
public Entity(){ public Entity(){
} }

@ -31,7 +31,7 @@ public class EntityBox extends Entity{
if(flyLength > 0){ if(flyLength > 0){
//can hit enemies while in air //can hit enemies while in air
for(Entity e: lvl.getEntities()){ 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.x = 0;
velocity.y = 0; velocity.y = 0;
e.stun(4f); e.stun(4f);
@ -41,8 +41,6 @@ public class EntityBox extends Entity{
} }
public void draw(Graphics g) { 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) if(shouldDrawSelf)
draw(g, 0, 0); draw(g, 0, 0);

@ -47,6 +47,14 @@ public class EntityPlayer extends Entity implements LightEmitter{
if(e.collision.overlaps(collision) && e.isChuckable){ if(e.collision.overlaps(collision) && e.isChuckable){
holding = e; holding = e;
holding.shouldDrawSelf = false; holding.shouldDrawSelf = false;
if(holding instanceof LightEmitter){
LightEmitter le = (LightEmitter)holding;
System.err.println(le.getLight());
}
break; break;
} }
} }

@ -28,6 +28,7 @@ public class EntityTorch extends EntityBox implements LightEmitter{
public EntityTorch(float x, float y) { public EntityTorch(float x, float y) {
super(x, y); super(x, y);
emitter = new Light(x + collision.width / 2, y + collision.height / 2, .3f); emitter = new Light(x + collision.width / 2, y + collision.height / 2, .3f);
emitter.setLightColor(1, .9f, .7f);
weight = 0; weight = 0;
} }

Loading…
Cancel
Save