Skip to content

Commit

Permalink
CompareLoaders: add 7 asset groups from glTF-Sample-Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Mar 13, 2024
1 parent b4d4b24 commit 1330bfd
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jar.manifest.attributes('Main-Class': application.mainClass)

dependencies {
implementation acorusCoordinates
implementation gsonCoordinates
implementation heartCoordinates
implementation wesCoordinates
implementation 'org.jmonkeyengine:jme3-plugins:' + jme3Version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,20 @@ private static void addAssetGroups() {
addAssetGroup("bats-props-blend", new BatsGroup("props", "blend"));
addAssetGroup("bats-props-glb", new BatsGroup("props", "glb"));

addAssetGroup("gltf-sample-assets", new GltfSampleAssets("glTF"));
addAssetGroup("gltf-sample-assets-binary",
new GltfSampleAssets("glTF-Binary"));
addAssetGroup("gltf-sample-assets-draco",
new GltfSampleAssets("glTF-Draco"));
addAssetGroup("gltf-sample-assets-embedded",
new GltfSampleAssets("glTF-Embedded"));
addAssetGroup("gltf-sample-assets-ibl",
new GltfSampleAssets("glTF-IBL"));
addAssetGroup("gltf-sample-assets-ktx-basisu",
new GltfSampleAssets("glTF-KTX-BasisU"));
addAssetGroup("gltf-sample-assets-quantized",
new GltfSampleAssets("glTF-Quantized"));

addAssetGroup("gltf-sample-models-10",
new GltfSampleModels("1.0", "glTF"));
addAssetGroup("gltf-sample-models-10-binary",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
Copyright (c) 2023-2024 Stephen Gold
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.stephengold.wrench.test;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jme3.asset.AssetLoadException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.MyString;

/**
* AssetGroup for a subset of the glTF sample assets at
* https://github.com/KhronosGroup/glTF-Sample-Assets
*
* @author Stephen Gold [email protected]
*/
class GltfSampleAssets implements AssetGroup {
// *************************************************************************
// constants and loggers

/**
* message logger for this class
*/
final private static Logger logger
= Logger.getLogger(GltfSampleAssets.class.getName());
// *************************************************************************
// fields

/**
* true if accessible, otherwise false
*/
final private boolean isAccessible;
/**
* metadata for the available assets
*/
final private Collection<SampleAsset> assets;
/**
* JSON parser
*/
final private static Gson parser = new Gson();
/**
* filesystem path to the asset root (the "Models" directory)
*/
private String rootPath;
/**
* selected asset variant ("glTF", "glTF-Binary", "glTF-Draco",
* "glTF-Embedded", "glTF-IBL", "glTF-KTX-BasisU", or "glTF-Quantized")
*/
final private String variant;
/**
* names of the assets in ascending lexicographic order (not empty)
*/
final private String[] namesArray;
// *************************************************************************
// constructors

/**
* Instantiate a group for the specified variant, ignoring tags.
*
* @param variant which asset variant ("glTF", "glTF-Binary", "glTF-Draco",
* "glTF-Embedded", "glTF-IBL", "glTF-KTX-BasisU", or "glTF-Quantized")
*/
GltfSampleAssets(String variant) {
this(new TreeSet<String>(), variant);
}

/**
* Instantiate a group for the specified tags and variant.
*
* @param requiredTags tags for filtering (may include "core", "extension",
* "issues", "showcase", "testing", or "written")
* @param variant which asset variant ("glTF", "glTF-Binary", "glTF-Draco",
* "glTF-Embedded", "glTF-IBL", "glTF-KTX-BasisU", or "glTF-Quantized")
*/
GltfSampleAssets(Set<String> requiredTags, String variant) {
switch (variant) {
case "glTF":
case "glTF-Binary":
case "glTF-Draco":
case "glTF-Embedded":
case "glTF-IBL":
case "glTF-KTX-BasisU":
case "glTF-Quantized":
this.variant = variant;
break;

default:
throw new IllegalArgumentException("varioant = " + variant);
}

this.rootPath = String.format("../../ext/glTF-Sample-Assets/Models");
String fileSeparator = System.getProperty("file.separator");
this.rootPath = rootPath.replace("/", fileSeparator);

// Test for accessibility of the JSON file:
File jsonFile = new File(rootPath, "model-index.json");
this.isAccessible = jsonFile.isFile() && jsonFile.canRead();
if (!isAccessible) {
String cwd = System.getProperty("user.dir");
logger.log(Level.WARNING, "{0} is not accessible from {1}.",
new Object[]{
MyString.quote(rootPath), MyString.quote(cwd)
});
}

// Open the JSON file as an InputStream:
InputStream stream;
try {
stream = new FileInputStream(jsonFile);
} catch (FileNotFoundException exception) {
throw new AssetLoadException(
"Failed to open file: " + jsonFile, exception);
}

// Read the JSON data into one long text string:
Charset charset = StandardCharsets.UTF_8;
String charsetName = charset.name();
String jsonString;
try (Scanner scanner = new Scanner(stream, charsetName)) {
scanner.useDelimiter("\\Z");
jsonString = scanner.next();
} catch (Exception exception) {
throw new AssetLoadException(
"Failed to read file: " + jsonFile, exception);
}

// Parse the JSON string into a collection:
TypeToken<Collection<SampleAsset>> assetCollection
= new TypeToken<Collection<SampleAsset>>() {
};
this.assets = parser.fromJson(jsonString, assetCollection.getType());

// Populate the array of asset names:
Set<String> nameSet = new TreeSet<>();
for (SampleAsset asset : assets) {
if (!asset.hasTags(requiredTags)) {
continue;
}
if (asset.providesVariant(variant)) {
nameSet.add(asset.name());
}
}
int numNames = nameSet.size();
if (numNames == 0) {
this.namesArray = null;
return;
}

this.namesArray = new String[numNames];
nameSet.toArray(namesArray);
}
// *************************************************************************
// AssetGroup methods

/**
* Return the path to the specified asset.
*
* @param assetName the name of the asset (not null)
* @return the asset path (not null)
*/
@Override
public String assetPath(String assetName) {
for (SampleAsset asset : assets) {
if (asset.name().equals(assetName)) {
String fileName = asset.fileName(variant);
String result = String.format(
"%s/%s/%s", assetName, variant, fileName);

return result;
}
}

throw new RuntimeException("assetName = " + assetName);
}

/**
* Test whether this group is accessible.
*
* @return true if readable, otherwise false
*/
@Override
public boolean isAccessible() {
return isAccessible;
}

/**
* Enumerate the asset names.
*
* @return a pre-existing array of asset names (not null, all elements
* non-null, in ascending lexicographic order)
*/
@Override
public String[] listAssets() {
return namesArray;
}

/**
* Return the asset root for the specified asset.
*
* @param assetName the name of the asset (not null)
* @return a filesystem path (not null, not empty)
*/
@Override
public String rootPath(String assetName) {
return rootPath;
}
}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ ext {
wesCoordinates = 'com.github.stephengold:Wes:0.8.1'

// production versions of libraries:
gsonCoordinates = 'com.google.code.gson:gson:2.9.1'
imageioVersion = '3.10.1'
jme3Version = '3.6.1-stable'
lwjglVersion = '3.3.2'

// advanced versions of libraries:
//gsonCoordinates = 'com.google.code.gson:gson:2.10.1'
//jme3Version = '3.7.0-SNAPSHOT'
//lwjglVersion = '3.3.3'
//lwjglVersion = '3.3.4-SNAPSHOT'
Expand Down

0 comments on commit 1330bfd

Please sign in to comment.