Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions jme3-core/src/main/java/com/jme3/util/BufferInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of 'jMonkeyEngine' 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 OWNER 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.jme3.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class BufferInputStream extends InputStream {

ByteBuffer input;

public BufferInputStream(ByteBuffer input) {
this.input = input;
}

@Override
public int read() throws IOException {
if (input.remaining() == 0) return -1; else return input.get() & 0xff;
}

@Override
public int read(byte[] b) {
return read(b, 0, b.length);
}

@Override
public int read(byte[] b, int off, int len) {
if (b == null) throw new NullPointerException("b == null");
if (off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException();
if (len == 0) return 0;
if (!input.hasRemaining()) return -1;

int toRead = Math.min(len, input.remaining());
input.get(b, off, toRead);
return toRead;
}

@Override
public int available() {
return input.remaining();
}
}
15 changes: 13 additions & 2 deletions jme3-examples/src/main/java/jme3test/model/TestGltfLoading.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
*/
package jme3test.model;

import com.jme3.anim.AnimClip;
import com.jme3.anim.AnimComposer;
import com.jme3.anim.SkinningControl;
import com.jme3.app.*;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.asset.plugins.UrlLocator;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
Expand Down Expand Up @@ -82,6 +82,7 @@ public void simpleInitApp() {

String folder = System.getProperty("user.home");
assetManager.registerLocator(folder, FileLocator.class);
assetManager.registerLocator("https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/refs/heads/main/", UrlLocator.class);

// cam.setLocation(new Vector3f(4.0339394f, 2.645184f, 6.4627485f));
// cam.setRotation(new Quaternion(-0.013950467f, 0.98604023f, -0.119502485f, -0.11510504f));
Expand Down Expand Up @@ -152,7 +153,14 @@ public void simpleInitApp() {

// loadModel("Models/gltf/Corset/glTF/Corset.gltf", new Vector3f(0, -1, 0), 20f);
// loadModel("Models/gltf/BoxInterleaved/glTF/BoxInterleaved.gltf", new Vector3f(0, 0, 0), 1f);


// From url locator

// loadModel("Models/AnimatedColorsCube/glTF/AnimatedColorsCube.gltf", new Vector3f(0, 0f, 0), 0.1f);
// loadModel("Models/AntiqueCamera/glTF/AntiqueCamera.gltf", new Vector3f(0, 0, 0), 0.1f);
// loadModel("Models/AnimatedMorphCube/glTF/AnimatedMorphCube.gltf", new Vector3f(0, 0, 0), 0.1f);
// loadModel("Models/AnimatedMorphCube/glTF-Binary/AnimatedMorphCube.glb", new Vector3f(0, 0, 0), 0.1f);

probeNode.attachChild(assets.get(0));

ChaseCameraAppState chaseCam = new ChaseCameraAppState();
Expand Down Expand Up @@ -231,7 +239,10 @@ private void loadModel(String path, Vector3f offset, float scale) {
private void loadModel(String path, Vector3f offset, Vector3f scale) {
GltfModelKey k = new GltfModelKey(path);
//k.setKeepSkeletonPose(true);
long t = System.currentTimeMillis();
Spatial s = assetManager.loadModel(k);
System.out.println("Load time : " + (System.currentTimeMillis() - t) + " ms");

s.scale(scale.x, scale.y, scale.z);
s.move(offset);
assets.add(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
package com.jme3.scene.plugins.gltf;

import com.jme3.asset.AssetInfo;
import com.jme3.util.BufferUtils;
import com.jme3.util.LittleEndien;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -50,12 +52,12 @@ public class GlbLoader extends GltfLoader {
*/
private static final Logger logger = Logger.getLogger(GlbLoader.class.getName());

private ArrayList<byte[]> data = new ArrayList<>();
private ArrayList<ByteBuffer> data = new ArrayList<>();

@Override
public Object load(AssetInfo assetInfo) throws IOException {
data.clear();
LittleEndien stream = new LittleEndien(new DataInputStream(assetInfo.openStream()));
LittleEndien stream = new LittleEndien(new BufferedInputStream(assetInfo.openStream()));
/* magic */ stream.readInt();

int version = stream.readInt();
Expand All @@ -76,11 +78,11 @@ public Object load(AssetInfo assetInfo) throws IOException {
int chunkType = stream.readInt();
if (chunkType == JSON_TYPE) {
json = new byte[chunkLength];
stream.read(json);
GltfUtils.readToByteArray(stream, json, chunkLength);
} else {
byte[] bin = new byte[chunkLength];
stream.read(bin);
data.add(bin);
ByteBuffer buff = BufferUtils.createByteBuffer(chunkLength);
GltfUtils.readToByteBuffer(stream, buff, chunkLength);
data.add(buff);
}
//8 is the byte size of the 2 ints chunkLength and chunkType.
length -= chunkLength + 8;
Expand All @@ -93,7 +95,7 @@ public Object load(AssetInfo assetInfo) throws IOException {
}

@Override
protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
return data.get(bufferIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@
import static com.jme3.scene.plugins.gltf.GltfUtils.*;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.util.BufferInputStream;
import com.jme3.util.BufferUtils;
import com.jme3.util.IntMap;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
import java.io.*;
import java.net.URLDecoder;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.*;
import java.util.logging.Level;
Expand Down Expand Up @@ -109,7 +112,6 @@ public Object load(AssetInfo assetInfo) throws IOException {

protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws IOException {
try {
dataCache.clear();
info = assetInfo;
skinnedSpatials.clear();
rootNode = new Node();
Expand Down Expand Up @@ -181,6 +183,27 @@ protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws
throw new AssetLoadException("An error occurred loading " + assetInfo.getKey().getName(), e);
} finally {
stream.close();
dataCache.clear();
skinBuffers.clear();
skinnedSpatials.clear();
info = null;
docRoot = null;
rootNode = null;
defaultMat = null;
accessors = null;
bufferViews = null;
buffers = null;
scenes = null;
nodes = null;
meshes = null;
materials = null;
textures = null;
images = null;
samplers = null;
animations = null;
skins = null;
cameras = null;
useNormalsFlag = false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff can be pretty huge on big scenes, so leaving it there is pretty bad for devices with limited memory

}
}

Expand Down Expand Up @@ -553,11 +576,15 @@ public Object readBuffer(Integer bufferViewIndex, int byteOffset, int count, Obj
// Not sure it's useful for us, but I guess it's useful when you map data directly to the GPU.
// int target = getAsInteger(bufferView, "target", 0);

byte[] data = readData(bufferIndex);
ByteBuffer data = readData(bufferIndex);
data = customContentManager.readExtensionAndExtras("bufferView", bufferView, data);

if(!(data instanceof ByteBuffer)){
throw new IOException("Buffer data is not a NIO Buffer");
}

if (store == null) {
store = new byte[byteLength];
store = BufferUtils.createByteBuffer(byteLength);
}

if (count == -1) {
Expand All @@ -569,14 +596,40 @@ public Object readBuffer(Integer bufferViewIndex, int byteOffset, int count, Obj
return store;
}

public byte[] readData(int bufferIndex) throws IOException {
public Buffer viewBuffer(Integer bufferViewIndex, int byteOffset, int count,
int numComponents, VertexBuffer.Format originalFormat, VertexBuffer.Format targetFormat) throws IOException {
JsonObject bufferView = bufferViews.get(bufferViewIndex).getAsJsonObject();
Integer bufferIndex = getAsInteger(bufferView, "buffer");
assertNotNull(bufferIndex, "No buffer defined for bufferView " + bufferViewIndex);
int bvByteOffset = getAsInteger(bufferView, "byteOffset", 0);
Integer byteLength = getAsInteger(bufferView, "byteLength");
assertNotNull(byteLength, "No byte length defined for bufferView " + bufferViewIndex);
int byteStride = getAsInteger(bufferView, "byteStride", 0);

ByteBuffer data = readData(bufferIndex);
data = customContentManager.readExtensionAndExtras("bufferView", bufferView, data);

if(!(data instanceof ByteBuffer)){
throw new IOException("Buffer data is not a NIO Buffer");
}


if (count == -1) {
count = byteLength;
}

return GltfUtils.getBufferView(data, byteOffset + bvByteOffset, count, byteStride, numComponents, originalFormat, targetFormat );

}

public ByteBuffer readData(int bufferIndex) throws IOException {
assertNotNull(buffers, "No buffer defined");

JsonObject buffer = buffers.get(bufferIndex).getAsJsonObject();
String uri = getAsString(buffer, "uri");
Integer bufferLength = getAsInteger(buffer, "byteLength");
assertNotNull(bufferLength, "No byteLength defined for buffer " + bufferIndex);
byte[] data = (byte[]) fetchFromCache("buffers", bufferIndex, Object.class);
ByteBuffer data = (ByteBuffer) fetchFromCache("buffers", bufferIndex, Object.class);
if (data != null) {
return data;
}
Expand All @@ -588,12 +641,12 @@ public byte[] readData(int bufferIndex) throws IOException {
return data;
}

protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
byte[] data;
protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
ByteBuffer data;
if (uri != null) {
if (uri.startsWith("data:")) {
// base 64 embed data
data = Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1));
data = BufferUtils.createByteBuffer(Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1)));
} else {
// external file let's load it
String decoded = decodeUri(uri);
Expand All @@ -603,11 +656,11 @@ protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) thr
}

BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded);
InputStream input = (InputStream) info.getManager().loadAsset(key);
data = new byte[bufferLength];
try (DataInputStream dataStream = new DataInputStream(input)) {
dataStream.readFully(data);
try(InputStream input = (InputStream) info.getManager().loadAsset(key)){
data = BufferUtils.createByteBuffer(bufferLength);
GltfUtils.readToByteBuffer(input, data, bufferLength);
}

}
} else {
// no URI, this should not happen in a gltf file, only in glb files.
Expand Down Expand Up @@ -784,19 +837,23 @@ public Texture2D readImage(int sourceIndex, boolean flip) throws IOException {
if (uri == null) {
assertNotNull(bufferView, "Image " + sourceIndex + " should either have an uri or a bufferView");
assertNotNull(mimeType, "Image " + sourceIndex + " should have a mimeType");
byte[] data = (byte[]) readBuffer(bufferView, 0, -1, null, 1, VertexBuffer.Format.Byte);
ByteBuffer data = (ByteBuffer) viewBuffer(bufferView, 0, -1, 1, VertexBuffer.Format.Byte, VertexBuffer.Format.Byte);

String extension = mimeType.split("/")[1];
TextureKey key = new TextureKey("image" + sourceIndex + "." + extension, flip);
result = (Texture2D) info.getManager().loadAssetFromStream(key, new ByteArrayInputStream(data));

try(BufferedInputStream bis = new BufferedInputStream(new BufferInputStream(data))){
result = (Texture2D) info.getManager().loadAssetFromStream(key, bis);
}
} else if (uri.startsWith("data:")) {
// base64 encoded image
String[] uriInfo = uri.split(",");
byte[] data = Base64.getDecoder().decode(uriInfo[1]);
ByteBuffer data = BufferUtils.createByteBuffer(Base64.getDecoder().decode(uriInfo[1]));
String headerInfo = uriInfo[0].split(";")[0];
String extension = headerInfo.split("/")[1];
TextureKey key = new TextureKey("image" + sourceIndex + "." + extension, flip);
result = (Texture2D) info.getManager().loadAssetFromStream(key, new ByteArrayInputStream(data));
try(BufferedInputStream bis = new BufferedInputStream(new BufferInputStream(data))){
result = (Texture2D) info.getManager().loadAssetFromStream(key, bis);
}
} else {
// external file image
String decoded = decodeUri(uri);
Expand Down Expand Up @@ -1338,13 +1395,14 @@ public VertexBuffer populate(Integer bufferViewIndex, int componentType, String
}
int numComponents = getNumberOfComponents(type);

Buffer buff = VertexBuffer.createBuffer(format, numComponents, count);
int bufferSize = numComponents * count;
Buffer buff;
if (bufferViewIndex == null) {
buff = VertexBuffer.createBuffer(format, numComponents, count);
// no referenced buffer, specs says to pad the buffer with zeros.
padBuffer(buff, bufferSize);
} else {
readBuffer(bufferViewIndex, byteOffset, count, buff, numComponents, originalFormat);
buff = (Buffer) viewBuffer(bufferViewIndex, byteOffset, count, numComponents, originalFormat, format);
}

if (bufferType == VertexBuffer.Type.Index) {
Expand Down
Loading
Loading