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
#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;
}
//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;
}

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"?>
<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">
<image source="env.png" width="128" height="128"/>
</tileset>
@ -392,11 +392,7 @@
</object>
<object id="161" x="720" y="57" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="162" x="768" y="57" width="16" height="16">
<properties>
<property name="customColor" value="0,1,0"/>
<property name="type" value="torch"/>
</properties>
</object>
@ -565,5 +561,11 @@
<property name="type" value="bat"/>
</properties>
</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>
</map>

@ -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());
}

@ -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);
}
}

@ -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);

@ -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;
}
}

@ -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(){
}

@ -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);

@ -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;
}
}

@ -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;
}

Loading…
Cancel
Save