Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambient and Directional light updates #53

Merged
merged 10 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions commons/src/main/com/mbrlabs/mundus/commons/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public Scene() {
cam.far = 10000;

DirectionalLight dirLight = new DirectionalLight();
dirLight.color.set(1, 1, 1, 1);
dirLight.intensity = 1f;
dirLight.direction.set(0, -1f, 0);
dirLight.color.set(DirectionalLight.DEFAULT_COLOR);
dirLight.intensity = DirectionalLight.DEFAULT_INTENSITY;
dirLight.direction.set(DirectionalLight.DEFAULT_DIRECTION);
dirLight.direction.nor();
environment.add(dirLight);
environment.getAmbientLight().intensity = 0.3f;
environment.getAmbientLight().intensity = 0.8f;

sceneGraph = new SceneGraph(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mbrlabs.mundus.commons.dto;

import com.badlogic.gdx.math.Vector3;

public class DirectionalLightDTO extends BaseLightDTO {
private Vector3 direction;

public Vector3 getDirection() {
return direction;
}

public void setDirection(Vector3 direction) {
this.direction = direction;
}
}
9 changes: 9 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/dto/SceneDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SceneDTO implements Json.Serializable {
private Array<GameObjectDTO> gameObjects;
private FogDTO fog;
private BaseLightDTO ambientLight;
private DirectionalLightDTO directionalLight;
private float camPosX;
private float camPosY;
private float camPosZ;
Expand Down Expand Up @@ -126,6 +127,14 @@ public void setAmbientLight(BaseLightDTO ambientLight) {
this.ambientLight = ambientLight;
}

public DirectionalLightDTO getDirectionalLight() {
return directionalLight;
}

public void setDirectionalLight(DirectionalLightDTO directionalLight) {
this.directionalLight = directionalLight;
}

public Array<GameObjectDTO> getGameObjects() {
return gameObjects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package com.mbrlabs.mundus.commons.env.lights;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector3;

/**
* @author Marcus Brummer
* @version 14-02-2016
*/
public class DirectionalLight extends BaseLight {
public static final float DEFAULT_INTENSITY = 1.0f;
public static final Color DEFAULT_COLOR = Color.WHITE.cpy();
public static final Vector3 DEFAULT_DIRECTION = new Vector3(0.40f, -1, 0);

public final Vector3 direction = new Vector3();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mbrlabs.mundus.commons.mapper;

import com.badlogic.gdx.graphics.Color;
import com.mbrlabs.mundus.commons.dto.DirectionalLightDTO;
import com.mbrlabs.mundus.commons.env.lights.DirectionalLight;

public class DirectionalLightConverter {

/**
* Converts {@link DirectionalLightDTO} to {@link DirectionalLight}.
*/
public static DirectionalLight convert(DirectionalLightDTO dto) {
if (dto == null) return null;
DirectionalLight light = new DirectionalLight();
light.intensity = dto.getIntensity();
light.color.set(dto.getColor());
light.direction.set(dto.getDirection());

return light;
}

/**
* Converts {@link DirectionalLight} to {@link DirectionalLightDTO}.
*/
public static DirectionalLightDTO convert(DirectionalLight light) {
if (light == null) return null;
DirectionalLightDTO lightDescriptor = new DirectionalLightDTO();
lightDescriptor.setIntensity(light.intensity);
lightDescriptor.setColor(Color.rgba8888(light.color));
lightDescriptor.setDirection(light.direction);

return lightDescriptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ public class WaterShader extends BaseShader {
protected final int UNIFORM_CAM_FAR_PLANE= register(new Uniform("u_camFarPlane"));

// ============================ LIGHTS ============================
protected final int UNIFORM_LIGHT_POS = register(new Uniform("u_lightPositon"));
protected final int UNIFORM_LIGHT_COLOR = register(new Uniform("u_lightColor"));
protected final int UNIFORM_AMBIENT_LIGHT_COLOR = register(new Uniform("u_ambientLight.color"));
protected final int UNIFORM_AMBIENT_LIGHT_INTENSITY = register(new Uniform("u_ambientLight.intensity"));
protected final int UNIFORM_DIRECTIONAL_LIGHT_COLOR = register(new Uniform("u_directionalLight.color"));
protected final int UNIFORM_DIRECTIONAL_LIGHT_DIR = register(new Uniform("u_directionalLight.direction"));
protected final int UNIFORM_DIRECTIONAL_LIGHT_INTENSITY = register(new Uniform("u_directionalLight.intensity"));

// ============================ FOG ============================
protected final int UNIFORM_FOG_DENSITY = register(new Uniform("u_fogDensity"));
Expand Down Expand Up @@ -104,16 +107,7 @@ public void begin(Camera camera, RenderContext context) {
public void render(Renderable renderable) {
final MundusEnvironment env = (MundusEnvironment) renderable.environment;

// directional lights
final DirectionalLightsAttribute dirLightAttribs = env.get(DirectionalLightsAttribute.class,
DirectionalLightsAttribute.Type);
final Array<DirectionalLight> dirLights = dirLightAttribs == null ? null : dirLightAttribs.lights;
if (dirLights != null && dirLights.size > 0) {
final DirectionalLight light = dirLights.first();
set(UNIFORM_LIGHT_COLOR, light.color.r, light.color.g, light.color.b);
// Normally this would be position but directional lights do not have a position
set(UNIFORM_LIGHT_POS, light.direction);
}
setLights(env);

// Set Textures
WaterTextureAttribute dudvAttrib = (WaterTextureAttribute) renderable.material.get(WaterTextureAttribute.Dudv);
Expand Down Expand Up @@ -179,6 +173,27 @@ public void render(Renderable renderable) {
renderable.meshPart.render(program);
}

private void setLights(MundusEnvironment env) {
// ambient
set(UNIFORM_AMBIENT_LIGHT_COLOR, env.getAmbientLight().color);
set(UNIFORM_AMBIENT_LIGHT_INTENSITY, env.getAmbientLight().intensity);

// TODO light array for each light type

// directional lights
final DirectionalLightsAttribute dirLightAttribs = env.get(DirectionalLightsAttribute.class,
DirectionalLightsAttribute.Type);
final Array<DirectionalLight> dirLights = dirLightAttribs == null ? null : dirLightAttribs.lights;
if (dirLights != null && dirLights.size > 0) {
final DirectionalLight light = dirLights.first();
set(UNIFORM_DIRECTIONAL_LIGHT_COLOR, light.color);
set(UNIFORM_DIRECTIONAL_LIGHT_DIR, light.direction);
set(UNIFORM_DIRECTIONAL_LIGHT_INTENSITY, light.intensity);
}

// TODO point lights, spot lights
}

private void setFloatUniform(Renderable renderable, long attribute, int uniform, float defaultValue) {
WaterFloatAttribute attr = (WaterFloatAttribute) renderable.material.get(attribute);
if (attr != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@ uniform float u_foamFallOffDistance;
uniform float u_foamScrollSpeed;
uniform float u_camNearPlane;
uniform float u_camFarPlane;
uniform vec3 u_lightColor;
varying vec2 v_diffuseUV;
uniform vec4 u_fogColor;

struct AmbientLight {
vec4 color;
float intensity;
};
uniform AmbientLight u_ambientLight;

struct DirectionalLight {
vec4 color;
vec3 direction;
float intensity;
};
uniform DirectionalLight u_directionalLight;

const vec4 COLOR_TURQUOISE = vec4(0,0.5,0.686, 0.2);

//https://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
Expand Down Expand Up @@ -84,10 +96,10 @@ void main() {
float refractiveFactor = dot(viewVector, normal);

// Calculate specular hightlights
vec3 reflectedLight = reflect(normalize(v_fromLightVector), normal);
vec3 reflectedLight = reflect(normalize(u_directionalLight.direction), normal);
float specular = max(dot(reflectedLight, viewVector), 0.0);
specular = pow(specular, u_shineDamper);
vec3 specularHighlights = u_lightColor * specular * u_reflectivity * clamp(waterDepth/5.0, 0.0, 1.0);
vec3 specularHighlights = vec3(u_directionalLight.color) * u_directionalLight.intensity * specular * u_reflectivity * clamp(waterDepth/5.0, 0.0, 1.0);

vec4 color = mix(reflectColor, refractColor, refractiveFactor);
color = mix(color, COLOR_TURQUOISE, 0.2) + vec4(specularHighlights, 0.0);
Expand Down Expand Up @@ -134,6 +146,9 @@ void main() {
// Fog
color = mix(color, u_fogColor, v_fog);

// ambient light
color *= u_ambientLight.color * u_ambientLight.intensity;

gl_FragColor = color;
//gl_FragColor = vec4(waterDepth/50.0);
//gl_FragColor.a = clamp(waterDepth/5.0, 0.0, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ uniform mat4 u_transMatrix;
uniform mat4 u_projViewMatrix;
uniform vec3 u_cameraPosition;
uniform float u_tiling;
uniform vec3 u_lightPositon;
uniform vec4 u_diffuseUVTransform;
varying vec2 v_diffuseUV;
uniform float u_fogDensity;
uniform float u_fogGradient;

struct DirectionalLight {
vec4 color;
vec3 direction;
float intensity;
};
uniform DirectionalLight u_directionalLight;

varying vec2 v_texCoord0;
varying vec2 v_waterTexCoords;
varying vec4 v_clipSpace;
Expand All @@ -31,7 +37,7 @@ void main() {
v_waterTexCoords = vec2(a_position.x/2.0 + 0.5, a_position.z/2.0 + 0.5) * u_tiling;

v_toCameraVector = u_cameraPosition - worldPos.xyz;
v_fromLightVector = worldPos.xyz - u_lightPositon; // from light to water
v_fromLightVector = worldPos.xyz - u_directionalLight.direction; // from light to water

// fog
if(u_fogDensity > 0.0 && u_fogGradient > 0.0) {
Expand Down
1 change: 1 addition & 0 deletions editor/CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[0.3.1] ~ TBD
- Added functionality to the editor to generate the assets.txt file, PR #45
- Added Ambient Light and Directional Light to Environment menu
- Fix NPE in editor when creating new terrain
- Fix NPE on model placement
- Fix Wave speed increasing exponentially based on water tile count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.mbrlabs.mundus.commons.dto.GameObjectDTO;
import com.mbrlabs.mundus.commons.dto.SceneDTO;
import com.mbrlabs.mundus.commons.env.lights.BaseLight;
import com.mbrlabs.mundus.commons.env.lights.DirectionalLight;
import com.mbrlabs.mundus.commons.env.lights.DirectionalLightsAttribute;
import com.mbrlabs.mundus.commons.mapper.BaseLightConverter;
import com.mbrlabs.mundus.commons.mapper.DirectionalLightConverter;
import com.mbrlabs.mundus.commons.mapper.FogConverter;
import com.mbrlabs.mundus.commons.scene3d.GameObject;
import com.mbrlabs.mundus.commons.scene3d.SceneGraph;
Expand Down Expand Up @@ -54,6 +57,11 @@ public static SceneDTO convert(Scene scene) {
// environment stuff
dto.setFog(FogConverter.convert(scene.environment.getFog()));
dto.setAmbientLight(BaseLightConverter.convert(scene.environment.getAmbientLight()));

DirectionalLightsAttribute directionalLight = scene.environment.get(DirectionalLightsAttribute.class, DirectionalLightsAttribute.Type);
dto.setDirectionalLight(DirectionalLightConverter.convert(directionalLight.lights.first()));

// Water
dto.setWaterResolution(scene.waterResolution);
dto.setWaterHeight(scene.waterHeight);

Expand Down Expand Up @@ -85,6 +93,13 @@ public static EditorScene convert(SceneDTO dto, Map<String, Asset> assets) {
scene.environment.setAmbientLight(ambientLight);
}

DirectionalLight light = DirectionalLightConverter.convert(dto.getDirectionalLight());
if (light != null) {
DirectionalLightsAttribute directionalLight = scene.environment.get(DirectionalLightsAttribute.class, DirectionalLightsAttribute.Type);
directionalLight.lights.clear();
directionalLight.lights.add(light);
}

// Water stuff
scene.waterResolution = dto.getWaterResolution();
if (scene.waterResolution == null)
Expand Down
1 change: 1 addition & 0 deletions editor/src/main/com/mbrlabs/mundus/editor/ui/UI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ object UI : Stage(ScreenViewport()) {
val fogDialog: FogDialog = FogDialog()
val skyboxDialog: SkyboxDialog = SkyboxDialog()
val ambientLightDialog: AmbientLightDialog = AmbientLightDialog()
var directionalLightDialog: DirectionalLightsDialog = DirectionalLightsDialog()
val exitDialog: ExitDialog = ExitDialog()

// styles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.kotcrab.vis.ui.util.FloatDigitsOnlyFilter
import com.kotcrab.vis.ui.widget.VisLabel
import com.kotcrab.vis.ui.widget.VisTextField
import com.kotcrab.vis.ui.widget.color.ColorPickerAdapter
Expand Down Expand Up @@ -62,13 +63,14 @@ class AmbientLightDialog : BaseDialog("Ambient Light"), ProjectChangedEvent.Proj
}

private fun setupListeners() {
val projectContext = projectManager.current()

// intensity
intensity.textFieldFilter = FloatDigitsOnlyFilter(false)
intensity.addListener(object : ChangeListener() {
override fun changed(event: ChangeListener.ChangeEvent, actor: Actor) {
override fun changed(event: ChangeEvent, actor: Actor) {
val d = convert(intensity.text)
if (d != null) {
val projectContext = projectManager.current()
projectContext.currScene.environment.ambientLight.intensity = d
}
}
Expand All @@ -77,7 +79,18 @@ class AmbientLightDialog : BaseDialog("Ambient Light"), ProjectChangedEvent.Proj
// color
colorPickerField.colorAdapter = object: ColorPickerAdapter() {
override fun finished(newColor: Color) {
projectContext.currScene.environment.ambientLight.color.set(color)
val projectContext = projectManager.current()
projectContext.currScene.environment.ambientLight.color.set(newColor)
}

override fun changed(newColor: Color?) {
val projectContext = projectManager.current()
projectContext.currScene.environment.ambientLight.color.set(newColor)
}

override fun canceled(oldColor: Color?) {
val projectContext = projectManager.current()
projectContext.currScene.environment.ambientLight.color.set(oldColor)
}
}

Expand Down
Loading