Skip to content

Commit

Permalink
Merge pull request #33 from jpooleycodes/skybox-enhancements
Browse files Browse the repository at this point in the history
Skybox enhancements
  • Loading branch information
JamesTKhan authored May 11, 2022
2 parents def9d36 + 0f1bb84 commit 11c979d
Show file tree
Hide file tree
Showing 15 changed files with 588 additions and 77 deletions.
1 change: 1 addition & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class Scene implements Disposable {
public SceneGraph sceneGraph;
public MundusEnvironment environment;
public Skybox skybox;
public String skyboxAssetId;
public float waterHeight = 0f;
public WaterResolution waterResolution = WaterResolution.DEFAULT_WATER_RESOLUTION;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public Asset findAssetByID(String id) {
return assetIndex.get(id);
}

/**
* Returns an asset by filename, else null if not found.
* @param fileName the filename to search
* @return matching asset or null
*/
public Asset findAssetByFileName(String fileName) {
for (Asset asset : assets) {
if (asset.file.name().equals(fileName))
return asset;
}
return null;
}

public Map<String, Asset> getAssetMap() {
return assetIndex;
}
Expand Down Expand Up @@ -239,32 +252,41 @@ public Asset loadAsset(Meta meta) throws MetaFileParseException, AssetNotFoundEx
// load actual asset
Asset asset = null;
switch (meta.getType()) {
case TEXTURE:
asset = loadTextureAsset(meta, assetFile);
break;
case PIXMAP_TEXTURE:
asset = loadPixmapTextureAsset(meta, assetFile);
break;
case TERRAIN:
asset = loadTerrainAsset(meta, assetFile);
break;
case MODEL:
asset = loadModelAsset(meta, assetFile);
break;
case MATERIAL:
asset = loadMaterialAsset(meta, assetFile);
break;
case TEXTURE:
asset = loadTextureAsset(meta, assetFile);
break;
case PIXMAP_TEXTURE:
asset = loadPixmapTextureAsset(meta, assetFile);
break;
case TERRAIN:
asset = loadTerrainAsset(meta, assetFile);
break;
case MODEL:
asset = loadModelAsset(meta, assetFile);
break;
case MATERIAL:
asset = loadMaterialAsset(meta, assetFile);
break;
case WATER:
asset = loadWaterAsset(meta, assetFile);
break;
default:
return null;
asset = loadWaterAsset(meta, assetFile);
break;
case SKYBOX:
asset = loadSkyboxAsset(meta, assetFile);
break;
default:
return null;
}

addAsset(asset);
return asset;
}

private Asset loadSkyboxAsset(Meta meta, FileHandle assetFile) {
SkyboxAsset asset = new SkyboxAsset(meta, assetFile);
asset.load();
return asset;
}

private MaterialAsset loadMaterialAsset(Meta meta, FileHandle assetFile) {
MaterialAsset asset = new MaterialAsset(meta, assetFile);
asset.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ public enum AssetType {
/** Material file. Mundus material file contains material information. */
MATERIAL,
/** Water file. Contains data for water. */
WATER
WATER,
/** Skybox file. Holds reference to skybox textures. **/
SKYBOX
}
124 changes: 124 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/assets/SkyboxAsset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.mbrlabs.mundus.commons.assets;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.PropertiesUtils;
import com.mbrlabs.mundus.commons.assets.meta.Meta;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

public class SkyboxAsset extends Asset {

private static final ObjectMap<String, String> MAP = new ObjectMap<>();

// property keys
public static final String PROP_POSITIVE_X = "positiveX";
public static final String PROP_NEGATIVE_X = "negativeX";
public static final String PROP_POSITIVE_Y = "positiveY";
public static final String PROP_NEGATIVE_Y = "negativeY";
public static final String PROP_POSITIVE_Z = "positiveZ";
public static final String PROP_NEGATIVE_Z = "negativeZ";

// ids of dependent assets
public String positiveXID;
public String negativeXID;
public String positiveYID;
public String negativeYID;
public String positiveZID;
public String negativeZID;

public TextureAsset positiveX;
public TextureAsset negativeX;
public TextureAsset positiveY;
public TextureAsset negativeY;
public TextureAsset positiveZ;
public TextureAsset negativeZ;

public SkyboxAsset(Meta meta, FileHandle assetFile) {
super(meta, assetFile);
}

@Override
public void load() {
MAP.clear();
try {
Reader reader = file.reader();
PropertiesUtils.load(MAP, reader);
reader.close();

// asset dependencies, load ids
positiveXID = MAP.get(PROP_POSITIVE_X, null);
negativeXID = MAP.get(PROP_NEGATIVE_X, null);

positiveYID = MAP.get(PROP_POSITIVE_Y, null);
negativeYID = MAP.get(PROP_NEGATIVE_Y, null);

positiveZID = MAP.get(PROP_POSITIVE_Z, null);
negativeZID = MAP.get(PROP_NEGATIVE_Z, null);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void resolveDependencies(Map<String, Asset> assets) {
if (assets.containsKey(positiveXID)) {
positiveX = (TextureAsset) assets.get(positiveXID);
}
if (assets.containsKey(negativeXID)) {
negativeX = (TextureAsset) assets.get(negativeXID);
}
if (assets.containsKey(positiveYID)) {
positiveY = (TextureAsset) assets.get(positiveYID);
}
if (assets.containsKey(negativeYID)) {
negativeY = (TextureAsset) assets.get(negativeYID);
}
if (assets.containsKey(positiveZID)) {
positiveZ = (TextureAsset) assets.get(positiveZID);
}
if (assets.containsKey(negativeZID)) {
negativeZ = (TextureAsset) assets.get(negativeZID);
}
}

@Override
public void applyDependencies() {
// not needed
}

@Override
public void dispose() {
positiveX.dispose();
negativeX.dispose();
positiveY.dispose();
negativeY.dispose();
positiveZ.dispose();
negativeZ.dispose();
}

@Override
public boolean usesAsset(Asset assetToCheck) {
if (assetToCheck == positiveX || assetToCheck == negativeX ||
assetToCheck == positiveY || assetToCheck == negativeY ||
assetToCheck == positiveZ || assetToCheck == negativeZ) {
return true;
}

return false;
}

/**
* Set TextureAsset Ids for the skybox. Useful for first initial creation of skybox
*/
public void setIds(String positiveX, String negativeX, String positiveY, String negativeY, String positiveZ, String negativeZ) {
this.positiveXID = positiveX;
this.negativeXID = negativeX;
this.positiveYID = positiveY;
this.negativeYID = negativeY;
this.positiveZID = positiveZ;
this.negativeZID = negativeZ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Meta {
public static final String JSON_TERRAIN = "ter";
public static final String JSON_WATER = "wat";
public static final String JSON_MODEL = "mdl";
public static final String JSON_SKY_BOX = "sky";

private int version;
private long lastModified;
Expand Down
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 @@ -29,6 +29,7 @@ public class SceneDTO {

private long id;
private String name;
private String skyboxAssetId;
private List<GameObjectDTO> gameObjects;
private FogDTO fog;
private BaseLightDTO ambientLight;
Expand Down Expand Up @@ -148,4 +149,12 @@ public void setWaterHeight(float waterHeight) {
public float getWaterHeight() {
return waterHeight;
}

public void setSkyboxAssetId(String skyboxAssetId) {
this.skyboxAssetId = skyboxAssetId;
}

public String getSkyboxAssetId() {
return skyboxAssetId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public class Skybox implements Disposable {
private FileHandle negativeZ;

public Skybox(FileHandle positiveX, FileHandle negativeX, FileHandle positiveY, FileHandle negativeY,
FileHandle positiveZ, FileHandle negativeZ) {
FileHandle positiveZ, FileHandle negativeZ, Shader shader) {
set(positiveX, negativeX, positiveY, negativeY, positiveZ, negativeZ);

this.shader = shader;
boxModel = createModel();
boxInstance = new ModelInstance(boxModel);
}
Expand Down
2 changes: 0 additions & 2 deletions editor/src/main/com/mbrlabs/mundus/editor/Editor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.mbrlabs.mundus.editor.events.SceneChangedEvent
import com.mbrlabs.mundus.editor.input.FreeCamController
import com.mbrlabs.mundus.editor.input.InputManager
import com.mbrlabs.mundus.editor.input.ShortcutController
import com.mbrlabs.mundus.editor.shader.Shaders
import com.mbrlabs.mundus.editor.tools.ToolManager
import com.mbrlabs.mundus.editor.ui.UI
import com.mbrlabs.mundus.editor.utils.Compass
Expand Down Expand Up @@ -113,7 +112,6 @@ class Editor : Lwjgl3WindowAdapter(), ApplicationListener,
val context = projectManager.current()
val scene = context.currScene
val sg = scene.sceneGraph
scene.skybox.shader = Shaders.skyboxShader

UI.sceneWidget.setCam(context.currScene.cam)
UI.sceneWidget.setRenderer {
Expand Down
Loading

0 comments on commit 11c979d

Please sign in to comment.