|
@@ -4,7 +4,7 @@ Namespace mojo3d.graphics
|
|
|
Private
|
|
|
|
|
|
Class Gltf2Loader
|
|
|
-
|
|
|
+
|
|
|
Method New( asset:Gltf2Asset,dir:String )
|
|
|
|
|
|
_asset=asset
|
|
@@ -95,43 +95,40 @@ Class Gltf2Loader
|
|
|
Return tex
|
|
|
End
|
|
|
|
|
|
- Method GetMaterial:Material( material:Gltf2Material )
|
|
|
+ Method GetMaterial:Material( material:Gltf2Material,textured:Bool )
|
|
|
|
|
|
If Not material Return New PbrMaterial( Color.Magenta )
|
|
|
|
|
|
If _materialCache.Contains( material ) Return _materialCache[material]
|
|
|
|
|
|
- Local mat:=New PbrMaterial
|
|
|
-
|
|
|
- Local texture:=GetTexture( material.baseColorTexture )
|
|
|
- If texture
|
|
|
- mat.ColorTexture=texture
|
|
|
+ Local colorTexture:Texture
|
|
|
+ Local metallicRoughnessTexture:Texture
|
|
|
+ Local occlusionTexture:Texture
|
|
|
+ Local emissiveTexture:Texture
|
|
|
+ Local normalTexture:Texture
|
|
|
+
|
|
|
+ If textured
|
|
|
+ colorTexture=GetTexture( material.baseColorTexture )
|
|
|
+ metallicRoughnessTexture=GetTexture( material.metallicRoughnessTexture )
|
|
|
+ occlusionTexture=GetTexture( material.occlusionTexture )
|
|
|
+ emissiveTexture=GetTexture( material.emissiveTexture )
|
|
|
+ normalTexture=GetTexture( material.normalTexture )
|
|
|
Endif
|
|
|
+
|
|
|
+ local bumpmapped:=normalTexture<>Null
|
|
|
+ Local boned:=False
|
|
|
+
|
|
|
+ Local mat:=New PbrMaterial( textured,bumpmapped,boned )
|
|
|
+
|
|
|
mat.ColorFactor=New Color( material.baseColorFactor.x,material.baseColorFactor.y,material.baseColorFactor.z )
|
|
|
-
|
|
|
- texture=GetTexture( material.emissiveTexture )
|
|
|
- If texture
|
|
|
- mat.EmissiveTexture=texture
|
|
|
- mat.EmissiveFactor=New Color( material.emissiveFactor.x,material.emissiveFactor.y,material.emissiveFactor.z )
|
|
|
- Endif
|
|
|
-
|
|
|
- texture=GetTexture( material.metallicRoughnessTexture )
|
|
|
- If texture
|
|
|
- mat.MetalnessTexture=texture
|
|
|
- mat.RoughnessTexture=texture
|
|
|
- Endif
|
|
|
mat.MetalnessFactor=material.metallicFactor
|
|
|
mat.RoughnessFactor=material.roughnessFactor
|
|
|
-
|
|
|
- texture=GetTexture( material.occlusionTexture )
|
|
|
- If texture
|
|
|
- mat.OcclusionTexture=texture
|
|
|
- Endif
|
|
|
-
|
|
|
- texture=GetTexture( material.normalTexture )
|
|
|
- If texture
|
|
|
- mat.NormalTexture=texture
|
|
|
- Endif
|
|
|
+
|
|
|
+ If colorTexture mat.ColorTexture=colorTexture
|
|
|
+ If metallicRoughnessTexture mat.MetalnessTexture=metallicRoughnessTexture ; mat.RoughnessTexture=metallicRoughnessTexture
|
|
|
+ If occlusionTexture mat.OcclusionTexture=occlusionTexture
|
|
|
+ If emissiveTexture mat.EmissiveTexture=emissiveTexture ; mat.EmissiveFactor=New Color( material.emissiveFactor.x,material.emissiveFactor.y,material.emissiveFactor.z )
|
|
|
+ If normalTexture mat.NormalTexture=normalTexture
|
|
|
|
|
|
_materialCache[material]=mat
|
|
|
Return mat
|
|
@@ -158,44 +155,73 @@ Class Gltf2Loader
|
|
|
|
|
|
'some sanity checking!
|
|
|
'
|
|
|
- If prim.mode<>4 Continue
|
|
|
+ If prim.mode<>4
|
|
|
+ Print "Gltf invalid mesh mode:"+prim.mode
|
|
|
+ Continue
|
|
|
+ Endif
|
|
|
|
|
|
- If Not prim.POSITION Or prim.POSITION.componentType<>5126 Or prim.POSITION.type<>"VEC3" DebugStop()
|
|
|
- If Not prim.NORMAL Or prim.NORMAL.componentType<>5126 Or prim.NORMAL.type<>"VEC3" DebugStop()
|
|
|
- If Not prim.TEXCOORD_0 Or prim.TEXCOORD_0.componentType<>5126 Or prim.TEXCOORD_0.type<>"VEC2" DebugStop()
|
|
|
- If Not prim.indices Or (prim.indices.componentType<>5123 And prim.indices.componentType<>5125) Or prim.indices.type<>"SCALAR" DebugStop()
|
|
|
+ If Not prim.indices
|
|
|
+ Print "Gltf mesh has no indices"
|
|
|
+ Continue
|
|
|
+ Endif
|
|
|
+
|
|
|
+
|
|
|
+ If prim.POSITION.componentType<>5126 Or prim.POSITION.type<>"VEC3"
|
|
|
+ Print "Gltf invalid nesh POSITION data"
|
|
|
+ Continue
|
|
|
+ Endif
|
|
|
+
|
|
|
+ If Not prim.indices Or (prim.indices.componentType<>5123 And prim.indices.componentType<>5125) Or prim.indices.type<>"SCALAR"
|
|
|
+ Print "Gltf invalid mesh indices data"
|
|
|
+ Continue
|
|
|
+ Endif
|
|
|
|
|
|
Local pp:=GetData( prim.POSITION )
|
|
|
Local pstride:=prim.POSITION.bufferView.byteStride
|
|
|
If Not pstride pstride=12
|
|
|
+
|
|
|
+ Local np:UByte Ptr,nstride:Int
|
|
|
+ If prim.NORMAL
|
|
|
+ If prim.NORMAL.componentType=5126 And prim.NORMAL.type="VEC3"
|
|
|
+ np=GetData( prim.NORMAL )
|
|
|
+ nstride=prim.NORMAL.bufferView.byteStride
|
|
|
+ If Not nstride nstride=12
|
|
|
+ Endif
|
|
|
+ Endif
|
|
|
|
|
|
- Local np:=GetData( prim.NORMAL )
|
|
|
- Local nstride:=prim.NORMAL.bufferView.byteStride
|
|
|
- If Not nstride nstride=12
|
|
|
+ Local tp:UByte Ptr,tstride:Int
|
|
|
+ If prim.TEXCOORD_0
|
|
|
+ If prim.TEXCOORD_0.componentType=5126 And prim.TEXCOORD_0.type="VEC2"
|
|
|
+ tp=GetData( prim.TEXCOORD_0 )
|
|
|
+ tstride=prim.TEXCOORD_0.bufferView.byteStride
|
|
|
+ If Not tstride tstride=8
|
|
|
+ Endif
|
|
|
+ Endif
|
|
|
|
|
|
- Local tp:=GetData( prim.TEXCOORD_0 )
|
|
|
- Local tstride:=prim.TEXCOORD_0.bufferView.byteStride
|
|
|
- If Not tstride tstride=8
|
|
|
-
|
|
|
Local vcount:=prim.POSITION.count
|
|
|
|
|
|
+ Print "gltf vcount="+vcount
|
|
|
+
|
|
|
Local vertices:=New Vertex3f[vcount],dstvp:=vertices.Data
|
|
|
|
|
|
For Local i:=0 Until vcount
|
|
|
|
|
|
dstvp[i].position=Cast<Vec3f Ptr>( pp )[0]
|
|
|
- dstvp[i].normal=Cast<Vec3f Ptr>( np )[0]
|
|
|
- dstvp[i].texCoord0=Cast<Vec2f Ptr>( tp )[0]
|
|
|
-
|
|
|
dstvp[i].position.z=-dstvp[i].position.z
|
|
|
- dstvp[i].normal.z=-dstvp[i].normal.z
|
|
|
-
|
|
|
dstvp[i].position=matrix * dstvp[i].position
|
|
|
- dstvp[i].normal=cofactor * dstvp[i].normal
|
|
|
-
|
|
|
pp+=pstride
|
|
|
- np+=nstride
|
|
|
- tp+=tstride
|
|
|
+
|
|
|
+ If np
|
|
|
+ dstvp[i].normal=Cast<Vec3f Ptr>( np )[0]
|
|
|
+ dstvp[i].normal.z=-dstvp[i].normal.z
|
|
|
+ dstvp[i].normal=cofactor * dstvp[i].normal
|
|
|
+ np+=nstride
|
|
|
+ Endif
|
|
|
+
|
|
|
+ If tp
|
|
|
+ dstvp[i].texCoord0=Cast<Vec2f Ptr>( tp )[0]
|
|
|
+ tp+=tstride
|
|
|
+ Endif
|
|
|
Next
|
|
|
|
|
|
Local icount:=prim.indices.count
|
|
@@ -231,7 +257,7 @@ Class Gltf2Loader
|
|
|
|
|
|
mesh.AddTriangles( indices,mesh.NumMaterials )
|
|
|
|
|
|
- materials.Push( GetMaterial( prim.material ) )
|
|
|
+ materials.Push( GetMaterial( prim.material,tp<>Null ) )
|
|
|
|
|
|
Else
|
|
|
|