Skip to content

Commit 084deae

Browse files
committed
Revert "Merge pull request #2553 from riccardobl/gltfimp"
This reverts commit 169f395, reversing changes made to fab757b.
1 parent f44d59a commit 084deae

File tree

5 files changed

+160
-479
lines changed

5 files changed

+160
-479
lines changed

jme3-core/src/main/java/com/jme3/util/BufferInputStream.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

jme3-examples/src/main/java/jme3test/model/TestGltfLoading.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
*/
3232
package jme3test.model;
3333

34+
import com.jme3.anim.AnimClip;
3435
import com.jme3.anim.AnimComposer;
3536
import com.jme3.anim.SkinningControl;
3637
import com.jme3.app.*;
3738
import com.jme3.asset.plugins.FileLocator;
38-
import com.jme3.asset.plugins.UrlLocator;
3939
import com.jme3.input.KeyInput;
4040
import com.jme3.input.controls.ActionListener;
4141
import com.jme3.input.controls.KeyTrigger;
@@ -82,7 +82,6 @@ public void simpleInitApp() {
8282

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

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

154153
// loadModel("Models/gltf/Corset/glTF/Corset.gltf", new Vector3f(0, -1, 0), 20f);
155154
// loadModel("Models/gltf/BoxInterleaved/glTF/BoxInterleaved.gltf", new Vector3f(0, 0, 0), 1f);
156-
157-
// From url locator
158-
159-
// loadModel("Models/AnimatedColorsCube/glTF/AnimatedColorsCube.gltf", new Vector3f(0, 0f, 0), 0.1f);
160-
// loadModel("Models/AntiqueCamera/glTF/AntiqueCamera.gltf", new Vector3f(0, 0, 0), 0.1f);
161-
// loadModel("Models/AnimatedMorphCube/glTF/AnimatedMorphCube.gltf", new Vector3f(0, 0, 0), 0.1f);
162-
// loadModel("Models/AnimatedMorphCube/glTF-Binary/AnimatedMorphCube.glb", new Vector3f(0, 0, 0), 0.1f);
163-
155+
164156
probeNode.attachChild(assets.get(0));
165157

166158
ChaseCameraAppState chaseCam = new ChaseCameraAppState();
@@ -239,10 +231,7 @@ private void loadModel(String path, Vector3f offset, float scale) {
239231
private void loadModel(String path, Vector3f offset, Vector3f scale) {
240232
GltfModelKey k = new GltfModelKey(path);
241233
//k.setKeepSkeletonPose(true);
242-
long t = System.currentTimeMillis();
243234
Spatial s = assetManager.loadModel(k);
244-
System.out.println("Load time : " + (System.currentTimeMillis() - t) + " ms");
245-
246235
s.scale(scale.x, scale.y, scale.z);
247236
s.move(offset);
248237
assets.add(s);

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GlbLoader.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@
3232
package com.jme3.scene.plugins.gltf;
3333

3434
import com.jme3.asset.AssetInfo;
35-
import com.jme3.util.BufferUtils;
3635
import com.jme3.util.LittleEndien;
3736

3837
import java.io.*;
39-
import java.nio.ByteBuffer;
4038
import java.util.ArrayList;
4139
import java.util.logging.Level;
4240
import java.util.logging.Logger;
@@ -52,12 +50,12 @@ public class GlbLoader extends GltfLoader {
5250
*/
5351
private static final Logger logger = Logger.getLogger(GlbLoader.class.getName());
5452

55-
private ArrayList<ByteBuffer> data = new ArrayList<>();
53+
private ArrayList<byte[]> data = new ArrayList<>();
5654

5755
@Override
5856
public Object load(AssetInfo assetInfo) throws IOException {
5957
data.clear();
60-
LittleEndien stream = new LittleEndien(new BufferedInputStream(assetInfo.openStream()));
58+
LittleEndien stream = new LittleEndien(new DataInputStream(assetInfo.openStream()));
6159
/* magic */ stream.readInt();
6260

6361
int version = stream.readInt();
@@ -78,11 +76,11 @@ public Object load(AssetInfo assetInfo) throws IOException {
7876
int chunkType = stream.readInt();
7977
if (chunkType == JSON_TYPE) {
8078
json = new byte[chunkLength];
81-
GltfUtils.readToByteArray(stream, json, chunkLength);
79+
stream.read(json);
8280
} else {
83-
ByteBuffer buff = BufferUtils.createByteBuffer(chunkLength);
84-
GltfUtils.readToByteBuffer(stream, buff, chunkLength);
85-
data.add(buff);
81+
byte[] bin = new byte[chunkLength];
82+
stream.read(bin);
83+
data.add(bin);
8684
}
8785
//8 is the byte size of the 2 ints chunkLength and chunkType.
8886
length -= chunkLength + 8;
@@ -95,7 +93,7 @@ public Object load(AssetInfo assetInfo) throws IOException {
9593
}
9694

9795
@Override
98-
protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
96+
protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
9997
return data.get(bufferIndex);
10098
}
10199

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@
4848
import static com.jme3.scene.plugins.gltf.GltfUtils.*;
4949
import com.jme3.texture.Texture;
5050
import com.jme3.texture.Texture2D;
51-
import com.jme3.util.BufferInputStream;
52-
import com.jme3.util.BufferUtils;
5351
import com.jme3.util.IntMap;
5452
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
5553
import java.io.*;
5654
import java.net.URLDecoder;
5755
import java.nio.Buffer;
58-
import java.nio.ByteBuffer;
5956
import java.nio.FloatBuffer;
6057
import java.util.*;
6158
import java.util.logging.Level;
@@ -112,6 +109,7 @@ public Object load(AssetInfo assetInfo) throws IOException {
112109

113110
protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws IOException {
114111
try {
112+
dataCache.clear();
115113
info = assetInfo;
116114
skinnedSpatials.clear();
117115
rootNode = new Node();
@@ -183,27 +181,6 @@ protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws
183181
throw new AssetLoadException("An error occurred loading " + assetInfo.getKey().getName(), e);
184182
} finally {
185183
stream.close();
186-
dataCache.clear();
187-
skinBuffers.clear();
188-
skinnedSpatials.clear();
189-
info = null;
190-
docRoot = null;
191-
rootNode = null;
192-
defaultMat = null;
193-
accessors = null;
194-
bufferViews = null;
195-
buffers = null;
196-
scenes = null;
197-
nodes = null;
198-
meshes = null;
199-
materials = null;
200-
textures = null;
201-
images = null;
202-
samplers = null;
203-
animations = null;
204-
skins = null;
205-
cameras = null;
206-
useNormalsFlag = false;
207184
}
208185
}
209186

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

579-
ByteBuffer data = readData(bufferIndex);
556+
byte[] data = readData(bufferIndex);
580557
data = customContentManager.readExtensionAndExtras("bufferView", bufferView, data);
581558

582-
if(!(data instanceof ByteBuffer)){
583-
throw new IOException("Buffer data is not a NIO Buffer");
584-
}
585-
586559
if (store == null) {
587-
store = BufferUtils.createByteBuffer(byteLength);
560+
store = new byte[byteLength];
588561
}
589562

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

599-
public Buffer viewBuffer(Integer bufferViewIndex, int byteOffset, int count,
600-
int numComponents, VertexBuffer.Format originalFormat, VertexBuffer.Format targetFormat) throws IOException {
601-
JsonObject bufferView = bufferViews.get(bufferViewIndex).getAsJsonObject();
602-
Integer bufferIndex = getAsInteger(bufferView, "buffer");
603-
assertNotNull(bufferIndex, "No buffer defined for bufferView " + bufferViewIndex);
604-
int bvByteOffset = getAsInteger(bufferView, "byteOffset", 0);
605-
Integer byteLength = getAsInteger(bufferView, "byteLength");
606-
assertNotNull(byteLength, "No byte length defined for bufferView " + bufferViewIndex);
607-
int byteStride = getAsInteger(bufferView, "byteStride", 0);
608-
609-
ByteBuffer data = readData(bufferIndex);
610-
data = customContentManager.readExtensionAndExtras("bufferView", bufferView, data);
611-
612-
if(!(data instanceof ByteBuffer)){
613-
throw new IOException("Buffer data is not a NIO Buffer");
614-
}
615-
616-
617-
if (count == -1) {
618-
count = byteLength;
619-
}
620-
621-
return GltfUtils.getBufferView(data, byteOffset + bvByteOffset, count, byteStride, numComponents, originalFormat, targetFormat );
622-
623-
}
624-
625-
public ByteBuffer readData(int bufferIndex) throws IOException {
572+
public byte[] readData(int bufferIndex) throws IOException {
626573
assertNotNull(buffers, "No buffer defined");
627574

628575
JsonObject buffer = buffers.get(bufferIndex).getAsJsonObject();
629576
String uri = getAsString(buffer, "uri");
630577
Integer bufferLength = getAsInteger(buffer, "byteLength");
631578
assertNotNull(bufferLength, "No byteLength defined for buffer " + bufferIndex);
632-
ByteBuffer data = (ByteBuffer) fetchFromCache("buffers", bufferIndex, Object.class);
579+
byte[] data = (byte[]) fetchFromCache("buffers", bufferIndex, Object.class);
633580
if (data != null) {
634581
return data;
635582
}
@@ -641,12 +588,12 @@ public ByteBuffer readData(int bufferIndex) throws IOException {
641588
return data;
642589
}
643590

644-
protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
645-
ByteBuffer data;
591+
protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException {
592+
byte[] data;
646593
if (uri != null) {
647594
if (uri.startsWith("data:")) {
648595
// base 64 embed data
649-
data = BufferUtils.createByteBuffer(Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1)));
596+
data = Base64.getDecoder().decode(uri.substring(uri.indexOf(",") + 1));
650597
} else {
651598
// external file let's load it
652599
String decoded = decodeUri(uri);
@@ -656,11 +603,11 @@ protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength)
656603
}
657604

658605
BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded);
659-
try(InputStream input = (InputStream) info.getManager().loadAsset(key)){
660-
data = BufferUtils.createByteBuffer(bufferLength);
661-
GltfUtils.readToByteBuffer(input, data, bufferLength);
606+
InputStream input = (InputStream) info.getManager().loadAsset(key);
607+
data = new byte[bufferLength];
608+
try (DataInputStream dataStream = new DataInputStream(input)) {
609+
dataStream.readFully(data);
662610
}
663-
664611
}
665612
} else {
666613
// no URI, this should not happen in a gltf file, only in glb files.
@@ -837,23 +784,19 @@ public Texture2D readImage(int sourceIndex, boolean flip) throws IOException {
837784
if (uri == null) {
838785
assertNotNull(bufferView, "Image " + sourceIndex + " should either have an uri or a bufferView");
839786
assertNotNull(mimeType, "Image " + sourceIndex + " should have a mimeType");
840-
ByteBuffer data = (ByteBuffer) viewBuffer(bufferView, 0, -1, 1, VertexBuffer.Format.Byte, VertexBuffer.Format.Byte);
841-
787+
byte[] data = (byte[]) readBuffer(bufferView, 0, -1, null, 1, VertexBuffer.Format.Byte);
842788
String extension = mimeType.split("/")[1];
843789
TextureKey key = new TextureKey("image" + sourceIndex + "." + extension, flip);
844-
try(BufferedInputStream bis = new BufferedInputStream(new BufferInputStream(data))){
845-
result = (Texture2D) info.getManager().loadAssetFromStream(key, bis);
846-
}
790+
result = (Texture2D) info.getManager().loadAssetFromStream(key, new ByteArrayInputStream(data));
791+
847792
} else if (uri.startsWith("data:")) {
848793
// base64 encoded image
849794
String[] uriInfo = uri.split(",");
850-
ByteBuffer data = BufferUtils.createByteBuffer(Base64.getDecoder().decode(uriInfo[1]));
795+
byte[] data = Base64.getDecoder().decode(uriInfo[1]);
851796
String headerInfo = uriInfo[0].split(";")[0];
852797
String extension = headerInfo.split("/")[1];
853798
TextureKey key = new TextureKey("image" + sourceIndex + "." + extension, flip);
854-
try(BufferedInputStream bis = new BufferedInputStream(new BufferInputStream(data))){
855-
result = (Texture2D) info.getManager().loadAssetFromStream(key, bis);
856-
}
799+
result = (Texture2D) info.getManager().loadAssetFromStream(key, new ByteArrayInputStream(data));
857800
} else {
858801
// external file image
859802
String decoded = decodeUri(uri);
@@ -1395,14 +1338,13 @@ public VertexBuffer populate(Integer bufferViewIndex, int componentType, String
13951338
}
13961339
int numComponents = getNumberOfComponents(type);
13971340

1341+
Buffer buff = VertexBuffer.createBuffer(format, numComponents, count);
13981342
int bufferSize = numComponents * count;
1399-
Buffer buff;
14001343
if (bufferViewIndex == null) {
1401-
buff = VertexBuffer.createBuffer(format, numComponents, count);
14021344
// no referenced buffer, specs says to pad the buffer with zeros.
14031345
padBuffer(buff, bufferSize);
14041346
} else {
1405-
buff = (Buffer) viewBuffer(bufferViewIndex, byteOffset, count, numComponents, originalFormat, format);
1347+
readBuffer(bufferViewIndex, byteOffset, count, buff, numComponents, originalFormat);
14061348
}
14071349

14081350
if (bufferType == VertexBuffer.Type.Index) {

0 commit comments

Comments
 (0)