
    ljB                       U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZ d dlmZmZmZmZ d dlZd dlZd dlmZ er1d dlmZ edegdf         Zded	<   edgdf         Zded
<   eez  Zded<   d dlmZmZ ddl m!Z! ddl"m#Z#  ee$          j%        dz  Z&i a'ded<   i Z(ded<   g dZ)d'dZ*d(dZ+ G d  d          Z, G d! d"e,          Z- G d# d$          Z. G d% d&e-          Z/dS ))    )annotationsN)CallableIteratorSequence)Path)TYPE_CHECKINGAnySelf	TypeAlias)OpenGLRendererObject3Dr   MeshTimeBasedUpdaterMeshNonTimeBasedUpdaterMeshUpdater)MatrixMNPoint3D   )config)openglshaderszdict[str, moderngl.Program]shader_program_cachezdict[Path, str]file_path_to_code_map)r   MeshShaderFullScreenQuad	file_pathr   returnstrc                d   | t           v rt           |          S |                                 }t          j        d|t          j                  }|D ]W}|                    d          }t          | j        |z            }|                    |                    d          |          }X|t           | <   |S )Nz%^#include (?P<include_path>.*\.glsl)$)flagsinclude_pathr   )	r   	read_textrefinditer	MULTILINEgroupget_shader_code_from_fileparentreplace)r   sourceinclude_linesmatchr!   included_codes         P/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/renderer/shader.pyr'   r'   '   s    )))$Y//  ""FK0l  M
  ? ?{{>221|+
 
 A>>'-)$M    unfiltered_attributesnpt.NDArray
attributesSequence[str]c                   g }t          | j        j                  D ]L\  }}||v rC|                    || j        |         j        d         j        | j        |         j        f           Mt          j        | | j        j        d                  j        d         |          }| j        j        D ]}||v r| |         ||<   |S )Nr   dtype)		enumerater6   namesappendsubdtyper   shapenpzeros)r0   r2   filtered_attributes_dtypei
dtype_namefiltered_attributess         r.   filter_attributesrB   :   s     !#"#8#>#DEE  :##%,,)/2;A>B)/28   (39?BCI!L'  
 ,17 P P
##.CJ.O
+r/   c                      e Zd Zd<dZd=dZd>dZd>dZd?dZddd@dZdAdZ	dBdZ
dCdZdDdZdEdZdFdZdFd ZdGd!ZdHdId$ZdJd&ZdKd'ZdLd)Z	 	 dMdNd0Z	 dOdPd2Z	 dOdQd4ZdRd5ZdSd6ZdTd8ZdSd9ZdUdVd:ZdSd;Zd*S )Wr   childrenc                    t          j        d          | _        t          j        d          | _        g | _        d | _         | j        |  |                                  d S )N   )r<   eyemodel_matrixnormal_matrixrD   r(   addinit_updaters)selfrD   s     r.   __init__zObject3D.__init__V   sW    F1IIVAYY(*'+(r/   startendalphafloat_r	   r   Nonec                z    d|z
  |j         z  ||j         z  z   | _         d|z
  |j        z  ||j        z  z   | _        d S )N   )rH   rI   )rL   rN   rO   rP   rR   s        r.   interpolatezObject3D.interpolate_   sP    Y%*<<usGW?WWI "'#*;";<r/   c                    t                      }| j                                        |_        | j                                        |_        |S N)r   rH   copyrI   rL   rY   s     r.   single_copyzObject3D.single_copye   s>    zz -2244!/4466r/   c                
   i }| g}|ru|                     d          }|                    |j                   |                                }|||<   |j        $|| ur ||j                                     |           |u||          S Nr   )popextendrD   r[   r(   rJ   )rL   node_to_copybfsnode	node_copys        r.   rY   zObject3D.copyk   s    f 		9771::DJJt}%%%((**I!*L {&4t+;+;T[)--i888  		9 D!!r/   c                    |D ]}|j         t          d           | j        |ddi | j                            |           |D ]	}| |_         
d S )Nz=Attempt to add child that's already added to another Object3Dcurrent_children_onlyF)r(   	ExceptionremoverD   r_   )rL   rD   childs      r.   rJ   zObject3D.add{   s     	 	E|'S   ( 	X;U;;;X&&& 	  	 EELL	  	 r/   T)re   re   boolc                   |rD ]}|j         | k    rt          d          t          t          fd| j                            | _        D ]	}d |_         
d S )Nz9Attempt to remove child that isn't added to this Object3Dc                    | vS rX    )rh   rD   s    r.   <lambda>z!Object3D.remove.<locals>.<lambda>   s    %x2G r/   )r(   rf   listfilterrD   )rL   re   rD   rh   s     ` r.   rg   zObject3D.remove   s      	!  <4''#S   ( V$G$G$G$GWWXX 	  	 EELL	  	 r/   r   c                4    | j         d d df         d d         S N   rH   rL   s    r.   get_positionzObject3D.get_position   s!     A&rr**r/   positionr
   c                2    || j         d d df         d d<   | S rq   rs   )rL   rv   s     r.   set_positionzObject3D.set_position   s%    &.!!!Q$#r/   Iterator[Mesh]c              #     K   | g}|rK|                                 }t          |t                    r|V  |                    |j                   |Id S d S rX   )r^   
isinstancer   r_   rD   rL   dfsr(   s      r.   
get_mesheszObject3D.get_meshes   so      f 	(WWYYF&$'' JJv'''	  	( 	( 	( 	( 	(r/   Iterator[Object3D]c              #     K   | g}|r6|                                 }|V  |                    |j                   |4d S d S rX   )r^   r_   rD   r|   s      r.   
get_familyzObject3D.get_family   s]      f 	(WWYYFLLLJJv'''  	( 	( 	( 	( 	(r/   c                    d S rX   rl   )rL   rR   s     r.   align_data_and_familyzObject3D.align_data_and_family   s    r/   r   c                   | j         | j        S | j        g}| }|j         -|                    |j         j                   |j         }|j         -t          j                            t          t          |                              S rX   )r(   rH   r9   r<   linalg	multi_dotrn   reversed)rL   model_matricescurrent_objects      r.   hierarchical_model_matrixz"Object3D.hierarchical_model_matrix   s    ;$$+,#/!!."7"DEEE+2N #/ y""4(@(@#A#ABBBr/   c                <   | j         | j        d dd df         S | j        g}| }|j         -|                    |j         j                   |j         }|j         -t          j                            t          t          |                              d dd df         S rq   )	r(   rI   r9   rH   r<   r   r   rn   r   )rL   normal_matricesr   s      r.   hierarchical_normal_matrixz#Object3D.hierarchical_normal_matrix   s    ;%bqb"1"f---.#/"">#8#EFFF+2N #/ y""4(A(A#B#BCCBQBFKKr/   c                >    g | _         g | _        d| _        d| _        d S )NF)time_based_updatersnon_time_updatershas_updatersupdating_suspendedrt   s    r.   rK   zObject3D.init_updaters   s(    ?A @B!"'r/   r   dtc                |    | j         r| j        r| S | j        D ]} || |           | j        D ]} ||            | S rX   )r   r   r   r   )rL   r   time_based_updaternon_time_based_updaters       r.   updatezObject3D.update   sq      	D$; 	K"&": 	) 	)tR((((&*&< 	) 	)"""4((((r/   list[MeshTimeBasedUpdater]c                    | j         S rX   )r   rt   s    r.   get_time_based_updatersz Object3D.get_time_based_updaters   s    ''r/   c                2    t          | j                  dk    S r]   )lenr   rt   s    r.   has_time_based_updaterzObject3D.has_time_based_updater   s    4+,,q00r/   list[MeshUpdater]c                     | j         | j        z   S rX   )r   r   rt   s    r.   get_updaterszObject3D.get_updaters   s    '$*@@@r/   Nupdate_functionr   index
int | Nonecall_updaterc                    dt          j        |          j        v r|                     ||           n|                     ||           |                                  |r|                                  | S )Nr   )inspect	signature
parameters_add_time_based_updater_add_non_time_updaterrefresh_has_updater_statusr   )rL   r   r   r   s       r.   add_updaterzObject3D.add_updater   sx     7$_55@@@((%@@@@&&>>>''))) 	KKMMMr/   r   c                x    || j                             |           d S | j                             ||           d S rX   )r   r9   insertrL   r   r   s      r.   r   z Object3D._add_time_based_updater   sE     =$++O<<<<<$++E?CCCCCr/   r   c                x    || j                             |           d S | j                             ||           d S rX   )r   r9   r   r   s      r.   r   zObject3D._add_non_time_updater   sE     ="))/:::::"))%AAAAAr/   c                    || j         v r#| j                             |           || j         v #|| j        v r#| j                            |           || j        v #|                                  | S rX   )r   rg   r   r   )rL   r   s     r.   remove_updaterzObject3D.remove_updater   s    !999$++O<<< !999!777"))/::: !777'')))r/   c                J    g | _         g | _        |                                  | S rX   )r   r   r   rt   s    r.   clear_updaterszObject3D.clear_updaters   s)    #% !#'')))r/   meshc                    |                                   |                                D ]}|                     |           | S rX   )r   r   r   )rL   r   updaters      r.   match_updaterszObject3D.match_updaters  sK    ((** 	& 	&GW%%%%r/   c                    d| _         | S NT)r   rt   s    r.   suspend_updatingzObject3D.suspend_updating  s    "&r/   c                D    d| _         |r|                     d           | S )NFr   )r   )r   r   )rL   r   s     r.   resume_updatingzObject3D.resume_updating  s+    "' 	KK1Kr/   c                Z    t          |                                           dk    | _        | S r]   )r   r   r   rt   s    r.   r   z#Object3D.refresh_has_updater_status  s(     1 1 3 344q8r/   )rD   r   )
rN   r   rO   r   rP   rQ   rR   r	   r   rS   )r   r   )rD   r   r   rS   )rD   r   re   ri   r   rS   )r   r   )rv   r   r   r
   )r   ry   )r   r   )rR   r	   r   rS   )r   r   r   rS   )r   )r   rQ   r   r
   )r   r   )r   ri   )r   r   r   )r   r   r   r   r   ri   r   r
   rX   )r   r   r   r   r   rS   )r   r   r   r   r   rS   )r   r   r   r
   )r   r
   )r   r   r   r
   )T)r   ri   r   r
   )__name__
__module____qualname__rM   rV   r[   rY   rJ   rg   ru   rx   r~   r   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   rl   r/   r.   r   r   U   s          < < < <   " " " " 	  	  	  	  IM 	  	  	  	  	  	 + + + +   ( ( ( (( ( ( (   	C 	C 	C 	C	L 	L 	L 	L( ( ( (    ( ( ( (1 1 1 1A A A A !!	    " JND D D D D MQB B B B B                     r/   c                  R     e Zd Zddddddej        fd fdZddZddZddZ xZ	S )r   NTshaderShader | Noner2   npt.NDArray | NonegeometryMesh | Nonematerialindicesuse_depth_testri   	primitiveintc                4   t                                                       |||| _        || _        || _        n3|"| || _        |j        | _        |j        | _        nt          d          || _        || _        d| _        | 	                                 d S )NzIMesh requires either attributes and a Shader or a Geometry and a MaterialF)
superrM   r   r2   r   rf   r   r   skip_renderrK   )	rL   r   r2   r   r   r   r   r   	__class__s	           r.   rM   zMesh.__init__  s     	*"8"(DK(DO"DLL!h&:"DK&1DO#+DLL   -"!&r/   r   c                J   t          | j                                        | j        | j        | j                                        nd | j        | j                  }| j        |_        | j                                        |_        | j	                                        |_	        |S )N)r2   r   r   r   r   )
r   r2   rY   r   r   r   r   r   rH   rI   rZ   s     r.   r[   zMesh.single_copy9  s    ++--;+/<+CDL%%'''.n
 
 
  + -2244!/4466r/   rendererr   rS   c                    | j                             dt          j        | j                             | j                             d|j        j                   | j                             d|j        j                   d S )Nu_model_matrixu_view_matrixu_projection_matrix)r   set_uniformr   matrix_to_shader_inputrH   cameraformatted_view_matrixprojection_matrix)rL   r   s     r.   set_uniformszMesh.set_uniformsG  s~    )$*;<<	
 	
 	
 	1VWWW!O-	
 	
 	
 	
 	
r/   c                   | j         rd S | j        r*| j        j                            t
          j                   n)| j        j                            t
          j                   g }| j        j        j	        
                                D ]4\  }}t          |t
          j                  r|                    |           5t          | j        |          }| j        j                            |                                          }| j        d }nP| j                            d                                          }|r | j        j                            |          }nd } | j        j        j        | j        j        |g|j        j        R d|i}|                    | j                   |                                 |                                 ||                                 d S d S )Ni4index_buffer)r   r   r   contextenablemoderngl
DEPTH_TESTdisableshader_program_membersitemsr{   	Attributer9   rB   r2   buffertobytesr   astypesimple_vertex_arrayr6   r8   renderr   release)	rL   shader_attribute_namesmember_namememberfiltered_shader_attributesvertex_buffer_objectindex_buffer_objectvert_index_datavertex_array_objects	            r.   r   zMesh.renderR  s    	F 	=K&&x':;;;;K''(;<<<,.#';#=#F#L#L#N#N 	; 	;K&("455 ;&--k:::%6O3&
 &
"  ${299&..00 
  
 <"&"l11$77??AAO +&*k&9&@&@&Q&Q##&*#Edk1EK& 
 (-3
 
 
 -	
 
 	""4>222$$&&&##%%%*''))))) +*r/   )r   r   r2   r   r   r   r   r   r   r   r   ri   r   r   )r   r   )r   r   r   rS   r   )
r   r   r   r   	TRIANGLESrM   r[   r   r   __classcell__r   s   @r.   r   r     s         !%)- $"&&*#!+      :   	
 	
 	
 	
&* &* &* &* &* &* &* &*r/   r   c                  $    e Zd Z	 	 dddZddZdS )r   Nr   moderngl.Contextname
str | Noner*   dict[str, Any] | Nonec                J   || _         || _        || _        | j        t          v r8t          | j                 j        | j         k    rt          | j                 | _        n| j         |j        di | j        | _        n{| j        ei }dddd}t          | j        z  }|                                D ]&}||z  }t          |          }	|	|||j
                 <   ' |j        di || _        nt          d          | j        $| j        t          vr| j        t          | j        <   d S d S d S )Nvertex_shaderfragment_shadergeometry_shader)vertfraggeom.Must either pass shader name or shader source.rl   )r   r   r*   r   ctxr   programSHADER_FOLDERiterdirr'   stemrf   )
rL   r   r   r*   source_dictsource_dict_keyshader_foldershader_fileshader_file_pathshader_sources
             r.   rM   zShader.__init__|  s\    	 I---$TY/3t|CC"6ty"AD[$"1'/"@"@DK"@"@DY"K')) O
 *DI5M,4466 T T#0;#>  9:J K KFSO,<,ABCC"1'/"@"@K"@"@DLMMM 9 TY6J%J%J.2.A +++ ! %J%Jr/   r   valuer	   r   rS   c                ~    t          j        t                    5  || j        |<   d d d            d S # 1 swxY w Y   d S rX   )
contextlibsuppressKeyErrorr   )rL   r   r  s      r.   r   zShader.set_uniform  s     ** 	. 	.(-D%	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   266NN)r   r   r   r   r*   r   )r   r   r  r	   r   rS   )r   r   r   rM   r   rl   r/   r.   r   r   {  sO          (,	'B 'B 'B 'B 'BR. . . . . .r/   r   c                  2     e Zd Z	 	 dd fdZd fd
Z xZS )r   Nr   r   fragment_shader_sourcer   fragment_shader_namec                   ||t          d          |t          | dz  }t          |          }n(|&t          j        |                                          }t          |d|d          }t          j        ddt          j	        dfg	          }t          j
        t          d
          t          d          ddgt          d
          t          d         ddgt          d
         t          d         ddgt          d
          t          d          ddgt          d
         t          d          ddgt          d
         t          d         ddgg          |d<   |                    dt          j                               |                    dt          j                               t!                                          ||           d S )Nr  z.fraga  
                #version 330
                in vec4 in_vert;
                uniform mat4 u_model_view_matrix;
                uniform mat4 u_projection_matrix;
                void main() {{
                    vec4 camera_space_vertex = u_model_view_matrix * in_vert;
                    vec4 clip_space_vertex = u_projection_matrix * camera_space_vertex;
                    gl_Position = clip_space_vertex;
                }}
                )r  r  )r*      in_vert)rF   r5   frame_x_radiusframe_y_radiusr   rU   u_model_view_matrixr   )rf   r
  r'   textwrapdedentlstripr   r<   r=   float32arrayr   r   r   view_matrixorthographic_projection_matrixr   rM   )rL   r   r  r  r  r   r2   r   s          r.   rM   zFullScreenQuad.__init__  s    ").B.JLMMM+,2F/M/M/MM%>?O%P%P""#/%-_5K5R5R5T5T%U%U"
" $: 
 
 
" XaBJ(E'FGGG
 ")**V4D-E,Eq!L)**F3C,DaK()62B+CQJ)**V4D-E,Eq!L()F3C,D+DaK()62B+CQJ	!
 	!

9 	0&2D2F2FGGG!133	
 	
 	
 	,,,,,r/   r   rS   c                H    t                                                       d S rX   )r   r   )rL   r   s    r.   r   zFullScreenQuad.render  s    r/   r  )r   r   r  r   r  r   r   )r   r   r   rM   r   r   r   s   @r.   r   r     si         .2+/	1- 1- 1- 1- 1- 1- 1-f         r/   r   )r   r   r   r   )r0   r1   r2   r3   r   r1   )0
__future__r   r  r   r#   r"  collections.abcr   r   r   pathlibr   typingr   r	   r
   r   r   numpyr<   numpy.typingnptmanim.renderer.opengl_rendererr   rQ   r   __annotations__r   r   manim.typingr   r    r   utilsr   __file__r(   r
  r   r   __all__r'   rB   r   r   r   r   rl   r/   r.   <module>r8     s   " " " " " " "      				  8 8 8 8 8 8 8 8 8 8       6 6 6 6 6 6 6 6 6 6 6 6            L======&.
E/BD/H&IIIII)1:,2D)EEEEE47KKKKKKK * * * * * * * *            X%	146  6 6 6 6)+  + + + +     &   6C C C C C C C CL]* ]* ]* ]* ]*8 ]* ]* ]*@,. ,. ,. ,. ,. ,. ,. ,.^5 5 5 5 5T 5 5 5 5 5r/   