Skip to content

Commit

Permalink
Merge pull request #49 from jpooleycodes/gwt-support
Browse files Browse the repository at this point in the history
Add GWT support
  • Loading branch information
JamesTKhan authored May 26, 2022
2 parents 440dd06 + cb08add commit f2ccee2
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 54 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ project(":commons") {
dependencies {
implementation "com.badlogicgames.gdx:gdx:$gdxVersion"

// Include HTML sources in commons as well for GWT support
api "com.github.mgsx-dev.gdx-gltf:gltf:$gltfVersion"
api "com.github.mgsx-dev.gdx-gltf:gltf:$gltfVersion:sources"

testImplementation "junit:junit:$junitVersion"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Marcus Brummer
* @version 06-10-2016
*/
@SuppressWarnings("NonJREEmulationClassesInClientCode")
public class AssetManager implements Disposable {

private static final String TAG = AssetManager.class.getSimpleName();
Expand Down Expand Up @@ -163,40 +164,33 @@ public Array<MaterialAsset> getMaterialAssets() {
* if a meta file can't be parsed
*/
public void loadAssets(AssetLoadingListener listener, boolean isRuntime) throws AssetNotFoundException, MetaFileParseException {
// create meta file filter
FileFilter metaFileFilter = new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(Meta.META_EXTENSION);
}
};

final MetaLoader metaLoader = new MetaLoader();

String[] files;
FileHandle fileList;
FileHandle[] metaFiles;

if (isRuntime && Gdx.app.getType() == Application.ApplicationType.Desktop) {
// Desktop applications cannot use .list() for internal jar files.
// Application will need to provide an assets.txt file listing all Mundus assets
// in the Mundus root directory.
// https://lyze.dev/2021/04/29/libGDX-Internal-Assets-List/
FileHandle fileList = rootFolder.child("assets.txt");
String[] files = fileList.readString().split("\\n");

// Get meta file extension file names
Array<String> metalFileNames = new Array<>();
for (String filename: files) {
if (filename.endsWith(Meta.META_EXTENSION)) {
metalFileNames.add(filename);
}
}

metaFiles = new FileHandle[metalFileNames.size];
for (int i = 0; i < metaFiles.length; i++) {
metaFiles[i] = rootFolder.child(metalFileNames.get(i));
}

fileList = rootFolder.child("assets.txt");
files = fileList.readString().split("\\n");
metaFiles = getMetaFiles(files);
} else if (isRuntime && Gdx.app.getType() == Application.ApplicationType.WebGL) {
// For WebGL we use a native split method for string split
fileList = rootFolder.child("assets.txt");
files = split(fileList.readString(), "\n");
metaFiles = getMetaFiles(files);
} else {
// Editor uses this block to load meta files
FileFilter metaFileFilter = new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(Meta.META_EXTENSION);
}
};
metaFiles = rootFolder.list(metaFileFilter);
}

Expand Down Expand Up @@ -228,6 +222,29 @@ public boolean accept(File file) {
}
}

/**
* Get an array of Meta FileHandles for the given String array of file names.
*
* @param files the array of file names to retrieve meta filehandles from
* @return FileHandle array of meta files.
*/
private FileHandle[] getMetaFiles(String[] files) {
// Get meta file extension file names
Array<String> metalFileNames = new Array<>();
for (String filename: files) {
if (filename.endsWith(Meta.META_EXTENSION)) {
metalFileNames.add(filename);
}
}

FileHandle[] metaFiles = new FileHandle[metalFileNames.size];
for (int i = 0; i < metaFiles.length; i++) {
metaFiles[i] = rootFolder.child(metalFileNames.get(i));
}

return metaFiles;
}

/**
* Loads an asset, given it's meta file.
*
Expand Down Expand Up @@ -336,6 +353,13 @@ public void dispose() {
assetIndex.clear();
}

/**
* Native JavaScript string split method for GWT support
*/
public static final native String[] split(String string, String separator) /*-{
return string.split(separator);
}-*/;

/**
* Used to inform users about the current loading status.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.mbrlabs.mundus.commons.assets;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.mbrlabs.mundus.commons.assets.meta.Meta;

import java.util.Map;
Expand Down Expand Up @@ -60,6 +62,28 @@ public void applyDependencies() {
// no dependencies here
}


/**
* Initiate loading of a Base64 string image into the pixmap.
*
* @param base64 a base64 encoded string of a PNG file
*/
public void loadBase64(String base64) {
Pixmap.downloadFromUrl(base64, new Pixmap.DownloadPixmapResponseListener() {
@Override
public void downloadComplete(Pixmap downloadedPixmap) {
pixmap = downloadedPixmap;
texture = new Texture(pixmap);
}

@Override
public void downloadFailed(Throwable t) {
Gdx.app.error(getClass().getName(), "Unable to Download Base64 Pixmap");
throw new GdxRuntimeException(t);
}
});
}

@Override
public void dispose() {
pixmap.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.mbrlabs.mundus.commons.assets;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.FloatArray;
Expand Down Expand Up @@ -171,6 +173,12 @@ public void resolveDependencies(Map<String, Asset> assets) {
String id = meta.getTerrain().getSplatmap();
if (id != null && assets.containsKey(id)) {
setSplatmap((PixmapTextureAsset) assets.get(id));

// If WebGL, we use base64 string for pixmap since pixmap cannot read from binaries on GWT
if (Gdx.app.getType() == Application.ApplicationType.WebGL) {
((PixmapTextureAsset) assets.get(id)).loadBase64(meta.getTerrain().getSplatBase64());
}

}

// splat channel base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void parseTerrain(Meta meta, JsonValue jsonTerrain) {
terrain.setSize(jsonTerrain.getInt(MetaTerrain.JSON_SIZE));
terrain.setUv(jsonTerrain.getFloat(MetaTerrain.JSON_UV_SCALE, Terrain.DEFAULT_UV_SCALE));
terrain.setSplatmap(jsonTerrain.getString(MetaTerrain.JSON_SPLATMAP, null));
terrain.setSplatBase64(jsonTerrain.getString(MetaTerrain.JSON_SPLAT_BASE64, null));
terrain.setSplatBase(jsonTerrain.getString(MetaTerrain.JSON_SPLAT_BASE, null));
terrain.setSplatR(jsonTerrain.getString(MetaTerrain.JSON_SPLAT_R, null));
terrain.setSplatG(jsonTerrain.getString(MetaTerrain.JSON_SPLAT_G, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class MetaTerrain {
public static final String JSON_SIZE = "size";
public static final String JSON_SPLATMAP = "map";
public static final String JSON_SPLAT_BASE = "base";
public static final String JSON_SPLAT_BASE64 = "base64";
public static final String JSON_SPLAT_R = "r";
public static final String JSON_SPLAT_G = "g";
public static final String JSON_SPLAT_B = "b";
Expand All @@ -36,6 +37,7 @@ public class MetaTerrain {
private float uv;
private String splatmap;
private String splatBase;
private String splatBase64;
private String splatR;
private String splatG;
private String splatB;
Expand All @@ -57,6 +59,14 @@ public void setSplatBase(String splatBase) {
this.splatBase = splatBase;
}

public String getSplatBase64() {
return splatBase64;
}

public void setSplatBase64(String splatBase64) {
this.splatBase64 = splatBase64;
}

public String getSplatR() {
return splatR;
}
Expand Down Expand Up @@ -109,13 +119,14 @@ public void setUv(float uv) {
public String toString() {
return "MetaTerrain{" +
"size=" + size +
", uv=" + uv +
", splatmap='" + splatmap + '\'' +
", splatBase='" + splatBase + '\'' +
", splatBase64='" + splatBase64 + '\'' +
", splatR='" + splatR + '\'' +
", splatG='" + splatG + '\'' +
", splatB='" + splatB + '\'' +
", splatA='" + splatA + '\'' +
", uv='" + uv + '\'' +
'}';
}
}
19 changes: 9 additions & 10 deletions commons/src/main/com/mbrlabs/mundus/commons/dto/GameObjectDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.mbrlabs.mundus.commons.dto;

import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.utils.Array;

/**
* @author Tibor Zsuro
Expand All @@ -31,16 +30,16 @@ public class GameObjectDTO {

private float[] transform = new float[10];

private List<String> tags;
private List<GameObjectDTO> childs;
private Array<String> tags;
private Array<GameObjectDTO> childs;

private ModelComponentDTO modelComponent;
private TerrainComponentDTO terrainComponent;
private WaterComponentDTO waterComponent;

public GameObjectDTO() {
childs = new ArrayList<>();
tags = new ArrayList<>();
childs = new Array<>();
tags = new Array<>();
}

public int getId() {
Expand Down Expand Up @@ -71,19 +70,19 @@ public float[] getTransform() {
return transform;
}

public List<String> getTags() {
public Array<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
public void setTags(Array<String> tags) {
this.tags = tags;
}

public List<GameObjectDTO> getChilds() {
public Array<GameObjectDTO> getChilds() {
return childs;
}

public void setChilds(List<GameObjectDTO> childs) {
public void setChilds(Array<GameObjectDTO> childs) {
this.childs = childs;
}

Expand Down
37 changes: 28 additions & 9 deletions commons/src/main/com/mbrlabs/mundus/commons/dto/SceneDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

package com.mbrlabs.mundus.commons.dto;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.mbrlabs.mundus.commons.water.WaterResolution;

import java.util.ArrayList;
import java.util.List;

/**
* @author Tibor Zsuro
* @version 12-08-2021
*/
public class SceneDTO {
public class SceneDTO implements Json.Serializable {

private long id;
private transient long id;
private String name;
private String skyboxAssetId;
private List<GameObjectDTO> gameObjects;
private Array<GameObjectDTO> gameObjects;
private FogDTO fog;
private BaseLightDTO ambientLight;
private float camPosX;
Expand All @@ -43,7 +43,7 @@ public class SceneDTO {
private WaterResolution waterResolution;

public SceneDTO() {
gameObjects = new ArrayList<>();
gameObjects = new Array<>();
}

public long getId() {
Expand Down Expand Up @@ -126,11 +126,11 @@ public void setAmbientLight(BaseLightDTO ambientLight) {
this.ambientLight = ambientLight;
}

public List<GameObjectDTO> getGameObjects() {
public Array<GameObjectDTO> getGameObjects() {
return gameObjects;
}

public void setGameObjects(List<GameObjectDTO> gameObjects) {
public void setGameObjects(Array<GameObjectDTO> gameObjects) {
this.gameObjects = gameObjects;
}

Expand All @@ -157,4 +157,23 @@ public void setSkyboxAssetId(String skyboxAssetId) {
public String getSkyboxAssetId() {
return skyboxAssetId;
}

@Override
public void write(Json json) {
// ID is written separately due to GWT technical limitations on Long emulation and reflection
json.writeValue("id", id);
json.writeFields(this);
}

@Override
public void read(Json json, JsonValue jsonData) {
json.setIgnoreUnknownFields(true);
// Default scenes may not have an ID, so we check for it first
if (jsonData.has("id")) {
// ID is read in separately due to GWT technical limitations on Long emulation and reflection
id = Long.parseLong(jsonData.getString("id"));
}
json.readFields(this, jsonData);
json.setIgnoreUnknownFields(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.shaders.BaseShader;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.mbrlabs.mundus.commons.env.Fog;
import com.mbrlabs.mundus.commons.env.MundusEnvironment;
Expand Down
Loading

0 comments on commit f2ccee2

Please sign in to comment.