@@ -393,7 +393,7 @@ def get_texture_for_mesh(
393
393
attributes = primitive ["attributes" ]
394
394
vertex_colors = self ._get_primitive_attribute (attributes , "COLOR_0" , np .float32 )
395
395
if vertex_colors is not None :
396
- return TexturesVertex (torch .from_numpy (vertex_colors ))
396
+ return TexturesVertex ([ torch .from_numpy (vertex_colors )] )
397
397
398
398
vertex_texcoords_0 = self ._get_primitive_attribute (
399
399
attributes , "TEXCOORD_0" , np .float32
@@ -559,12 +559,26 @@ def __init__(self, data: Meshes, buffer_stream: BinaryIO) -> None:
559
559
meshes = defaultdict (list )
560
560
# pyre-fixme[6]: Incompatible parameter type
561
561
meshes ["name" ] = "Node-Mesh"
562
- primitives = {
563
- "attributes" : {"POSITION" : 0 , "TEXCOORD_0" : 2 },
564
- "indices" : 1 ,
565
- "material" : 0 , # default material
566
- "mode" : _PrimitiveMode .TRIANGLES ,
567
- }
562
+ if isinstance (self .mesh .textures , TexturesVertex ):
563
+ primitives = {
564
+ "attributes" : {"POSITION" : 0 , "COLOR_0" : 2 },
565
+ "indices" : 1 ,
566
+ "mode" : _PrimitiveMode .TRIANGLES ,
567
+ }
568
+ elif isinstance (self .mesh .textures , TexturesUV ):
569
+ primitives = {
570
+ "attributes" : {"POSITION" : 0 , "TEXCOORD_0" : 2 },
571
+ "indices" : 1 ,
572
+ "mode" : _PrimitiveMode .TRIANGLES ,
573
+ "material" : 0 ,
574
+ }
575
+ else :
576
+ primitives = {
577
+ "attributes" : {"POSITION" : 0 },
578
+ "indices" : 1 ,
579
+ "mode" : _PrimitiveMode .TRIANGLES ,
580
+ }
581
+
568
582
meshes ["primitives" ].append (primitives )
569
583
self ._json_data ["meshes" ].append (meshes )
570
584
@@ -610,6 +624,14 @@ def _write_accessor_json(self, key: str) -> Tuple[int, np.ndarray]:
610
624
element_min = list (map (float , np .min (data , axis = 0 )))
611
625
element_max = list (map (float , np .max (data , axis = 0 )))
612
626
byte_per_element = 2 * _DTYPE_BYTES [_ITEM_TYPES [_ComponentType .FLOAT ]]
627
+ elif key == "texvertices" :
628
+ component_type = _ComponentType .FLOAT
629
+ data = self .mesh .textures .verts_features_list ()[0 ].cpu ().numpy ()
630
+ element_type = "VEC3"
631
+ buffer_view = 2
632
+ element_min = list (map (float , np .min (data , axis = 0 )))
633
+ element_max = list (map (float , np .max (data , axis = 0 )))
634
+ byte_per_element = 3 * _DTYPE_BYTES [_ITEM_TYPES [_ComponentType .FLOAT ]]
613
635
elif key == "indices" :
614
636
component_type = _ComponentType .UNSIGNED_SHORT
615
637
data = (
@@ -646,8 +668,10 @@ def _write_accessor_json(self, key: str) -> Tuple[int, np.ndarray]:
646
668
return (byte_length , data )
647
669
648
670
def _write_bufferview (self , key : str , ** kwargs ):
649
- if key not in ["positions" , "texcoords" , "indices" ]:
650
- raise ValueError ("key must be one of positions, texcoords or indices" )
671
+ if key not in ["positions" , "texcoords" , "texvertices" , "indices" ]:
672
+ raise ValueError (
673
+ "key must be one of positions, texcoords, texvertices or indices"
674
+ )
651
675
652
676
bufferview = {
653
677
"name" : "bufferView_%s" % key ,
@@ -661,6 +685,10 @@ def _write_bufferview(self, key: str, **kwargs):
661
685
byte_per_element = 2 * _DTYPE_BYTES [_ITEM_TYPES [_ComponentType .FLOAT ]]
662
686
target = _TargetType .ARRAY_BUFFER
663
687
bufferview ["byteStride" ] = int (byte_per_element )
688
+ elif key == "texvertices" :
689
+ byte_per_element = 3 * _DTYPE_BYTES [_ITEM_TYPES [_ComponentType .FLOAT ]]
690
+ target = _TargetType .ELEMENT_ARRAY_BUFFER
691
+ bufferview ["byteStride" ] = int (byte_per_element )
664
692
elif key == "indices" :
665
693
byte_per_element = (
666
694
3 * _DTYPE_BYTES [_ITEM_TYPES [_ComponentType .UNSIGNED_SHORT ]]
@@ -701,12 +729,15 @@ def save(self):
701
729
pos_byte , pos_data = self ._write_accessor_json ("positions" )
702
730
idx_byte , idx_data = self ._write_accessor_json ("indices" )
703
731
include_textures = False
704
- if (
705
- self .mesh .textures is not None
706
- and self .mesh .textures .verts_uvs_list ()[0 ] is not None
707
- ):
708
- tex_byte , tex_data = self ._write_accessor_json ("texcoords" )
709
- include_textures = True
732
+ if self .mesh .textures is not None :
733
+ if hasattr (self .mesh .textures , "verts_features_list" ):
734
+ tex_byte , tex_data = self ._write_accessor_json ("texvertices" )
735
+ include_textures = True
736
+ texcoords = False
737
+ elif self .mesh .textures .verts_uvs_list ()[0 ] is not None :
738
+ tex_byte , tex_data = self ._write_accessor_json ("texcoords" )
739
+ include_textures = True
740
+ texcoords = True
710
741
711
742
# bufferViews for positions, texture coords and indices
712
743
byte_offset = 0
@@ -717,17 +748,19 @@ def save(self):
717
748
byte_offset += idx_byte
718
749
719
750
if include_textures :
720
- self ._write_bufferview (
721
- "texcoords" , byte_length = tex_byte , offset = byte_offset
722
- )
751
+ if texcoords :
752
+ self ._write_bufferview (
753
+ "texcoords" , byte_length = tex_byte , offset = byte_offset
754
+ )
755
+ else :
756
+ self ._write_bufferview (
757
+ "texvertices" , byte_length = tex_byte , offset = byte_offset
758
+ )
723
759
byte_offset += tex_byte
724
760
725
761
# image bufferView
726
762
include_image = False
727
- if (
728
- self .mesh .textures is not None
729
- and self .mesh .textures .maps_list ()[0 ] is not None
730
- ):
763
+ if self .mesh .textures is not None and hasattr (self .mesh .textures , "maps_list" ):
731
764
include_image = True
732
765
image_byte , image_data = self ._write_image_buffer (offset = byte_offset )
733
766
byte_offset += image_byte
0 commit comments