@@ -616,3 +616,113 @@ def render():
616
616
verify_image (window .get_image_buffer_as_numpy (),
617
617
'test_draw_part_of_lines' )
618
618
window .destroy ()
619
+
620
+ @pytest .mark .skipif (not _ti_core .GGUI_AVAILABLE , reason = "GGUI Not Available" )
621
+ @test_utils .test (arch = supported_archs )
622
+ def test_draw_mesh_instances ():
623
+ N = 10
624
+ NV = (N + 1 )** 2
625
+ NT = 2 * N ** 2
626
+ NE = 2 * N * (N + 1 ) + N ** 2
627
+ pos = ti .Vector .field (3 , ti .f32 , shape = NV )
628
+ tri = ti .field (ti .i32 , shape = 3 * NT )
629
+ edge = ti .Vector .field (2 , ti .i32 , shape = NE )
630
+
631
+ # Instance Attribute Information
632
+ NInstanceRows = 100
633
+ NInstanceCols = 100
634
+ NInstance = NInstanceRows * NInstanceCols
635
+ instances_transforms = ti .Matrix .field (4 , 4 , ti .f32 , shape = (NInstance ,))
636
+
637
+ @ti .kernel
638
+ def init_transforms_of_instances ():
639
+ identity = ti .Matrix .identity (ti .f32 , 4 )
640
+ for i in range (NInstanceRows ):
641
+ for j in range (NInstanceCols ):
642
+ index = i * NInstanceCols + j
643
+ instances_transforms [index ] = identity
644
+ translate_matrix = ti .math .translate (1.2 * j , 0 , - 1.2 * i )
645
+ instances_transforms [index ] = translate_matrix @ instances_transforms [index ]
646
+
647
+ @ti .kernel
648
+ def init_pos ():
649
+ for i , j in ti .ndrange (N + 1 , N + 1 ):
650
+ idx = i * (N + 1 ) + j
651
+ pos [idx ] = ti .Vector ([i / N , 1.0 - j / N , 0.5 ])
652
+
653
+ @ti .kernel
654
+ def init_tri ():
655
+ for i , j in ti .ndrange (N , N ):
656
+ tri_idx = 6 * (i * N + j )
657
+ pos_idx = i * (N + 1 ) + j
658
+ if (i + j ) % 2 == 0 :
659
+ tri [tri_idx + 0 ] = pos_idx
660
+ tri [tri_idx + 1 ] = pos_idx + N + 2
661
+ tri [tri_idx + 2 ] = pos_idx + 1
662
+ tri [tri_idx + 3 ] = pos_idx
663
+ tri [tri_idx + 4 ] = pos_idx + N + 1
664
+ tri [tri_idx + 5 ] = pos_idx + N + 2
665
+ else :
666
+ tri [tri_idx + 0 ] = pos_idx
667
+ tri [tri_idx + 1 ] = pos_idx + N + 1
668
+ tri [tri_idx + 2 ] = pos_idx + 1
669
+ tri [tri_idx + 3 ] = pos_idx + 1
670
+ tri [tri_idx + 4 ] = pos_idx + N + 1
671
+ tri [tri_idx + 5 ] = pos_idx + N + 2
672
+
673
+ @ti .kernel
674
+ def init_edge ():
675
+ for i , j in ti .ndrange (N + 1 , N ):
676
+ edge_idx = i * N + j
677
+ pos_idx = i * (N + 1 ) + j
678
+ edge [edge_idx ] = ti .Vector ([pos_idx , pos_idx + 1 ])
679
+ start = N * (N + 1 )
680
+ for i , j in ti .ndrange (N , N + 1 ):
681
+ edge_idx = start + j * N + i
682
+ pos_idx = i * (N + 1 ) + j
683
+ edge [edge_idx ] = ti .Vector ([pos_idx , pos_idx + N + 1 ])
684
+ start = 2 * N * (N + 1 )
685
+ for i , j in ti .ndrange (N , N ):
686
+ edge_idx = start + i * N + j
687
+ pos_idx = i * (N + 1 ) + j
688
+ if (i + j ) % 2 == 0 :
689
+ edge [edge_idx ] = ti .Vector ([pos_idx , pos_idx + N + 2 ])
690
+ else :
691
+ edge [edge_idx ] = ti .Vector ([pos_idx + 1 , pos_idx + N + 1 ])
692
+
693
+
694
+ @ti .kernel
695
+ def update_transform (t : ti .f32 ):
696
+ for i in range (NInstance ):
697
+ rotation_matrix = ti .math .rot_by_axis (ti .math .vec3 (0 , 1 , 0 ), 0.01 * ti .math .sin (t ))
698
+ instances_transforms [i ] = instances_transforms [i ] @ rotation_matrix
699
+
700
+ init_transforms_of_instances ()
701
+
702
+ init_pos ()
703
+ init_tri ()
704
+ init_edge ()
705
+
706
+ window = ti .ui .Window ("test" , (1024 , 1024 ), vsync = True , show_window = False )
707
+ canvas = window .get_canvas ()
708
+ scene = ti .ui .Scene ()
709
+ camera = ti .ui .make_camera ()
710
+ camera .position (- 1.82731234 , 2.26492691 , 2.27800684 )
711
+ camera .lookat (- 1.13230401 , 2.11502124 , 1.57480579 )
712
+ camera .fov (90 )
713
+
714
+ def render ():
715
+ scene .set_camera (camera )
716
+ scene .point_light (pos = (0.5 , 1 , 2 ), color = (1 , 1 , 1 ))
717
+
718
+ scene .mesh_instance (pos , tri , color = (39 / 255 , 123 / 255 , 192 / 255 ), two_sided = True , transforms = instances_transforms )
719
+ canvas .scene (scene )
720
+
721
+ for i in range (30 ):
722
+ update_transform (30 )
723
+ render ()
724
+ window .get_image_buffer_as_numpy ()
725
+
726
+ render ()
727
+ verify_image (window .get_image_buffer_as_numpy (), 'test_draw_mesh_instances' )
728
+ window .destroy ()
0 commit comments