|
@@ -5,9 +5,10 @@ Private
|
|
|
|
|
|
Class Gltf2Loader
|
|
|
|
|
|
- Method New( asset:Gltf2Asset,dir:String )
|
|
|
+ Method New( asset:Gltf2Asset,bindata:DataBuffer,dir:String )
|
|
|
|
|
|
_asset=asset
|
|
|
+ _bindata=bindata
|
|
|
_dir=dir
|
|
|
End
|
|
|
|
|
@@ -72,8 +73,8 @@ Class Gltf2Loader
|
|
|
Field _dir:String
|
|
|
|
|
|
Field _data:=New Map<Gltf2Buffer,DataBuffer>
|
|
|
-
|
|
|
-' Field _uridata:=New StringMap<DataBuffer>
|
|
|
+ Field _uridata:=New StringMap<DataBuffer>
|
|
|
+ Field _bindata:DataBuffer
|
|
|
|
|
|
Field _textureCache:=New Map<Gltf2Texture,Texture>
|
|
|
Field _materialCache:=New Map<Gltf2Material,Material>
|
|
@@ -84,37 +85,38 @@ Class Gltf2Loader
|
|
|
|
|
|
Field _bones:Model.Bone[]
|
|
|
|
|
|
-#rem
|
|
|
- Method GetData:UByte Ptr( uri:String )
|
|
|
- Local data:=_data[uri]
|
|
|
- If Not data
|
|
|
- data=DataBuffer.Load( _dir+uri )
|
|
|
- _data[uri]=data
|
|
|
- Endif
|
|
|
- Return data.Data
|
|
|
- End
|
|
|
-#end
|
|
|
-
|
|
|
Method GetData:UByte Ptr( buffer:Gltf2Buffer )
|
|
|
|
|
|
If _data.Contains( buffer ) Return _data[buffer]?.Data
|
|
|
|
|
|
- Local data:DataBuffer
|
|
|
+ Local data:DataBuffer,uri:=buffer.uri
|
|
|
+
|
|
|
+ If Not uri
|
|
|
+
|
|
|
+ data=_bindata
|
|
|
|
|
|
- Local uri:=buffer.uri
|
|
|
- If uri.StartsWith( "data:" )
|
|
|
+ Else If uri.StartsWith( "data:" )
|
|
|
+
|
|
|
Local i:=uri.Find( ";base64," )
|
|
|
+
|
|
|
If i<>-1
|
|
|
Local base64:=uri.Slice( i+8 )
|
|
|
data=DecodeBase64( base64 )
|
|
|
Else
|
|
|
Print "Can't decode data:"
|
|
|
Endif
|
|
|
+
|
|
|
+ Else If _uridata.Contains( uri )
|
|
|
+
|
|
|
+ data=_uridata[uri]
|
|
|
Else
|
|
|
+
|
|
|
data=DataBuffer.Load( _dir+uri )
|
|
|
+ _uridata[uri]=data
|
|
|
Endif
|
|
|
|
|
|
_data[buffer]=data
|
|
|
+
|
|
|
Return data?.Data
|
|
|
End
|
|
|
|
|
@@ -660,14 +662,58 @@ Class Gltf2Mojo3dLoader Extends Mojo3dLoader
|
|
|
|
|
|
Private
|
|
|
|
|
|
- Method Open:Gltf2Loader( path:String )
|
|
|
+ Method Open:Gltf2Loader( path:String )
|
|
|
+
|
|
|
+ path=RealPath( path )
|
|
|
|
|
|
- If ExtractExt( path ).ToLower()<>".gltf" Return Null
|
|
|
+ Local asset:Gltf2Asset,bindata:DataBuffer
|
|
|
+
|
|
|
+ Select ExtractExt( path ).ToLower()
|
|
|
+ Case ".glb"
|
|
|
+
|
|
|
+ Local stream:=Stream.Open( path,"r" )
|
|
|
|
|
|
- Local asset:=Gltf2Asset.Load( path )
|
|
|
+ Local json:=""
|
|
|
+
|
|
|
+ If stream.ReadUInt()=$46546C67 '"GLTF"
|
|
|
+
|
|
|
+ Local version:=stream.ReadUInt()
|
|
|
+ Local length:=stream.ReadUInt()-12
|
|
|
+
|
|
|
+ While length>0
|
|
|
+
|
|
|
+ Local clength:=stream.ReadUInt()
|
|
|
+ Local ctype:=stream.ReadUInt()
|
|
|
+
|
|
|
+ Select ctype
|
|
|
+ Case $4E4F534A '"JSON"
|
|
|
+ Local buf:=stream.ReadAll( clength )
|
|
|
+ json=buf.PeekString( 0 )
|
|
|
+ buf.Discard()
|
|
|
+ Case $004E4942 '"BIN"
|
|
|
+ bindata=stream.ReadAll( clength )
|
|
|
+ End
|
|
|
+
|
|
|
+ length-=clength+8
|
|
|
+ Wend
|
|
|
+
|
|
|
+ Endif
|
|
|
+
|
|
|
+ stream.Close()
|
|
|
+
|
|
|
+ If Not json Return Null
|
|
|
+
|
|
|
+ asset=Gltf2Asset.Parse( json )
|
|
|
+
|
|
|
+ Case ".gltf"
|
|
|
+
|
|
|
+ asset=Gltf2Asset.Load( path )
|
|
|
+
|
|
|
+ End
|
|
|
+
|
|
|
If Not asset Return Null
|
|
|
|
|
|
- Local loader:=New Gltf2Loader( asset,ExtractDir( path ) )
|
|
|
+ Local loader:=New Gltf2Loader( asset,bindata,ExtractDir( path ) )
|
|
|
|
|
|
Return loader
|
|
|
End
|