@@ -1246,7 +1246,7 @@ public GltfSubObject(MeshPrimitive meshPrimitive, string name, int hierarchyInde
12461246 options |= SceneryMaterialOptions . PbrHasNormals ;
12471247
12481248 // Cannot proceed without TexCoord_0 neither, must add a dummy one.
1249- if ( ! meshPrimitive . Attributes . ContainsKey ( "TEXCOORD_0" ) )
1249+ if ( ! vertexAttributes . Any ( a => a . VertexBuffer . VertexDeclaration . GetVertexElements ( ) . Any ( e => e . VertexElementUsage == VertexElementUsage . TextureCoordinate && e . UsageIndex == 0 ) ) )
12501250 {
12511251 vertexAttributes . Add ( new VertexBufferBinding ( new VertexBuffer ( shape . Viewer . GraphicsDevice ,
12521252 new VertexDeclaration ( new VertexElement ( 0 , VertexElementFormat . NormalizedShort2 , VertexElementUsage . TextureCoordinate , 0 ) ) , vertexCount , BufferUsage . None ) { Name = "TEXCOORD_0_DUMMY" } ) ) ;
@@ -1257,45 +1257,44 @@ public GltfSubObject(MeshPrimitive meshPrimitive, string name, int hierarchyInde
12571257 {
12581258 options |= SceneryMaterialOptions . PbrHasTexCoord1 ; // With this we request to call the NormalMapColor pipeline
12591259
1260- if ( ! meshPrimitive . Attributes . ContainsKey ( "TANGENT" ) )
1260+ if ( ! vertexAttributes . Any ( a => a . VertexBuffer . VertexDeclaration . GetVertexElements ( ) . Any ( e => e . VertexElementUsage == VertexElementUsage . Tangent ) ) )
12611261 vertexAttributes . Add ( new VertexBufferBinding ( new VertexBuffer ( shape . Viewer . GraphicsDevice ,
12621262 new VertexDeclaration ( new VertexElement ( 0 , VertexElementFormat . Color , VertexElementUsage . Tangent , 0 ) ) , vertexCount , BufferUsage . WriteOnly )
12631263 { Name = "TANGENT_DUMMY" } ) ) ; // VertexElementFormat.Color is actually unsigned Byte4 normalized.
12641264 else if ( ! TangentsAlwaysCalculatedPerPixel )
12651265 options |= SceneryMaterialOptions . PbrHasTangents ;
12661266 }
1267- else if ( normalScale != 0 )
1267+
1268+ if ( meshPrimitive . Attributes . ContainsKey ( "TANGENT" ) )
12681269 {
1269- if ( meshPrimitive . Attributes . ContainsKey ( "TANGENT" ) )
1270+ // Per-pixel tangent calculation gives better result. In that case we are not using the provided Tangent vertex attribute at all, so it can be removed. (See BoomBox)
1271+ if ( TangentsAlwaysCalculatedPerPixel && ! meshPrimitive . Attributes . ContainsKey ( "COLOR_0" ) && ! meshPrimitive . Attributes . ContainsKey ( "TEXCOORD_1" )
1272+ && ( options & ( SceneryMaterialOptions . PbrHasSkin | SceneryMaterialOptions . PbrHasMorphTargets ) ) == 0 )
12701273 {
1271- // Per-pixel tangent calculation gives better result. In that case we are not using the provided Tangent vertex attribute at all, so it can be removed. (See BoomBox)
1272- if ( TangentsAlwaysCalculatedPerPixel )
1273- {
1274- var removables = vertexAttributes . Where ( b => {
1275- var ve = b . VertexBuffer . VertexDeclaration . GetVertexElements ( ) ;
1276- return ve . Count ( ) == 1 && ve . Any ( e => e . VertexElementUsage == VertexElementUsage . Tangent ) ;
1277- } ) ;
1278- foreach ( var r in removables )
1279- Disposables . Enqueue ( r . VertexBuffer ) ;
1280- if ( vertexAttributes . RemoveAll ( b => removables . Contains ( b ) ) == 0 )
1281- // Unsuccessful removal because the buffers are interleaved
1282- options |= SceneryMaterialOptions . PbrHasTangents ;
1283- }
1284- else
1274+ var removables = vertexAttributes . Where ( b => {
1275+ var ve = b . VertexBuffer . VertexDeclaration . GetVertexElements ( ) ;
1276+ return ve . Count ( ) == 1 && ve . Any ( e => e . VertexElementUsage == VertexElementUsage . Tangent ) ;
1277+ } ) ;
1278+ foreach ( var r in removables )
1279+ Disposables . Enqueue ( r . VertexBuffer ) ;
1280+ if ( vertexAttributes . RemoveAll ( b => removables . Contains ( b ) ) == 0 )
1281+ // Unsuccessful removal because the buffers are interleaved
12851282 options |= SceneryMaterialOptions . PbrHasTangents ;
12861283 }
1284+ else
1285+ options |= SceneryMaterialOptions . PbrHasTangents ;
12871286 }
12881287
12891288 // When we have a Tangent, must also make sure to have TexCoord_1 and Color_0
12901289 if ( ( options & ( SceneryMaterialOptions . PbrHasTangents | SceneryMaterialOptions . PbrHasSkin | SceneryMaterialOptions . PbrHasMorphTargets ) ) != 0
12911290 || meshPrimitive . Attributes . ContainsKey ( "COLOR_0" ) || meshPrimitive . Attributes . ContainsKey ( "TEXCOORD_1" ) )
12921291 {
1293- if ( ! meshPrimitive . Attributes . ContainsKey ( "TEXCOORD_1" ) )
1292+ if ( ! vertexAttributes . Any ( a => a . VertexBuffer . VertexDeclaration . GetVertexElements ( ) . Any ( e => e . VertexElementUsage == VertexElementUsage . TextureCoordinate && e . UsageIndex == 1 ) ) )
12941293 {
12951294 vertexAttributes . Add ( new VertexBufferBinding ( new VertexBuffer ( shape . Viewer . GraphicsDevice ,
12961295 new VertexDeclaration ( new VertexElement ( 0 , VertexElementFormat . NormalizedShort2 , VertexElementUsage . TextureCoordinate , 1 ) ) , vertexCount , BufferUsage . None ) { Name = "TEXCOORD_1_DUMMY" } ) ) ;
12971296 }
1298- if ( ! meshPrimitive . Attributes . ContainsKey ( "COLOR_0" ) )
1297+ if ( ! vertexAttributes . Any ( a => a . VertexBuffer . VertexDeclaration . GetVertexElements ( ) . Any ( e => e . VertexElementUsage == VertexElementUsage . Color ) ) )
12991298 {
13001299 var vertexBuffer = new VertexBuffer ( shape . Viewer . GraphicsDevice ,
13011300 new VertexDeclaration ( new VertexElement ( 0 , VertexElementFormat . Color , VertexElementUsage . Color , 0 ) ) , vertexCount , BufferUsage . None ) { Name = "COLOR_0_DUMMY" } ;
0 commit comments