4848import static com .jme3 .scene .plugins .gltf .GltfUtils .*;
4949import com .jme3 .texture .Texture ;
5050import com .jme3 .texture .Texture2D ;
51- import com .jme3 .util .BufferInputStream ;
52- import com .jme3 .util .BufferUtils ;
5351import com .jme3 .util .IntMap ;
5452import com .jme3 .util .mikktspace .MikktspaceTangentGenerator ;
5553import java .io .*;
5654import java .net .URLDecoder ;
5755import java .nio .Buffer ;
58- import java .nio .ByteBuffer ;
5956import java .nio .FloatBuffer ;
6057import java .util .*;
6158import 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