
    ^j8                       d dl mZ d dlZd dlZd dlZd dlmZmZmZmZ d dl	T  e
ed          oej        ZdZdZdZd	 Z G d
 dej                  Z G d dej                  Ze                    d            G d d          ZdS )    )annotationsN)clockeventgraphicsimage)*is_pyglet_doc_runa&  #version 150
    in vec3 position;
    in vec4 size;
    in vec4 scale;
    in vec4 velocity;
    in vec4 color_start;
    in vec4 color_end;
    in vec4 texture_uv;
    in float rotation;
    in float birth;

    out vec4 geo_size;
    out vec4 geo_scale;
    out vec4 geo_velocity;
    out vec4 geo_color_start;
    out vec4 geo_color_end;
    out vec4 geo_tex_coords;
    out float geo_rotation;
    out float geo_birth;
    out int geo_vert_id;

    void main() {
        gl_Position = vec4(position, 1);
        geo_size = size;
        geo_scale = scale;
        geo_velocity = velocity;
        geo_color_start = color_start;
        geo_color_end = color_end;
        geo_tex_coords = texture_uv;
        geo_rotation = rotation;
        geo_birth = birth;
        geo_vert_id = gl_VertexID;
    }
a6  #version 150
    // We are taking single points from the vertex shader
    // and emitting 4 new vertices to create a quad.
    layout (points) in;
    layout (triangle_strip, max_vertices = 32) out;

    uniform WindowBlock
    {
        mat4 projection;
        mat4 view;
    } window;

    uniform float time;

    // Since geometry shader can take multiple values from a vertex
    // shader we need to define the inputs from it as arrays.
    // For our purposes, we just take single values (points).
    in vec4 geo_size[];
    in vec4 geo_scale[];
    in vec4 geo_velocity[];
    in vec4 geo_color_start[];
    in vec4 geo_color_end[];
    in vec4 geo_tex_coords[];
    in float geo_rotation[];
    in float geo_birth[];
    in int geo_vert_id[];

    out vec2 uv;
    out vec4 frag_color;

    void main() {
        // Unpack the image size and anchor
        vec2 size = geo_size[0].xy;
        vec2 anchor = geo_size[0].zw;
        vec2 scale_start = geo_scale[0].xy;
        vec2 scale_end = geo_scale[0].zw;
        
        vec2 velocity = geo_velocity[0].xy;
        vec2 spread = geo_velocity[0].zw;

        float birth = geo_birth[0];
        float elapsed = time - birth;
        float repeater = mod(elapsed, 1.0);

        int vert_id = geo_vert_id[0];

        for(int i=0;i<8;++i){
            // TODO: user supplied rotation speed
            float time_scale = mod(elapsed - (i / 7.0), 1.0);
            float rotation = geo_rotation[0] + time_scale * 100;

            // TODO: user supplied X, Y velocities
            vec3 center = gl_in[0].gl_Position.xyz;
            center.x += time_scale * velocity.x * (spread.x * cos(vert_id + 1) * sin(i + 1));
            center.y += time_scale * velocity.y * (spread.y * sin(vert_id + 1) * cos(i + 1));
            
            // Interpolate between the start and end colors, based on the lifetime 
            // (end - start) * step + start
            frag_color = (geo_color_end[0] - geo_color_start[0]) * time_scale + geo_color_start[0]; 
    
            // Interpolate between the start and end scale, based on the lifetime 
            // (end - start) * step + start
            mat4 m_scale = mat4(1.0);
            m_scale[0][0] = ((scale_end - scale_start) * time_scale + scale_start).x;
            m_scale[1][1] = ((scale_end - scale_start) * time_scale + scale_start).y;

            // This matrix controls the actual position of the particles:
            mat4 m_translate = mat4(1.0);
            m_translate[3][0] = center.x;
            m_translate[3][1] = center.y;
            m_translate[3][2] = center.z;

            mat4 m_rotation = mat4(1.0);
            m_rotation[0][0] =  cos(radians(-rotation)); 
            m_rotation[0][1] =  sin(radians(-rotation));
            m_rotation[1][0] = -sin(radians(-rotation));
            m_rotation[1][1] =  cos(radians(-rotation));    
    
            // Final UV coords (left, bottom, right, top):
            float uv_l = geo_tex_coords[0].s;
            float uv_b = geo_tex_coords[0].t;
            float uv_r = geo_tex_coords[0].p;
            float uv_t = geo_tex_coords[0].q;
    
            // Emit a triangle strip to create a quad (4 vertices).
            // Prepare and reuse the transformation matrix and fragment color:
            mat4 m_pv = window.projection * window.view * m_translate * m_rotation * m_scale;

            // Upper left
            gl_Position = m_pv * vec4(vec2(0.0, size.y) - anchor, 0.0, 1.0);
            uv = vec2(uv_l, uv_t);
            EmitVertex();
    
            // lower left
            gl_Position = m_pv * vec4(vec2(0.0, 0.0) - anchor, 0.0, 1.0);
            uv = vec2(uv_l, uv_b);
            EmitVertex();
    
            // upper right
            gl_Position = m_pv * vec4(vec2(size.x, size.y) - anchor, 0.0, 1.0);
            uv = vec2(uv_r, uv_t);
            EmitVertex();
    
            // lower right
            gl_Position = m_pv * vec4(vec2(size.x, 0.0) - anchor, 0.0, 1.0);
            uv = vec2(uv_r, uv_b);
            EmitVertex();

            // We are done with this triangle strip now
            EndPrimitive();    

        }
    }
z#version 150
    in vec2 uv;
    in vec4 frag_color;
    out vec4 final_color;

    uniform sampler2D particle_texture;

    void main() {
        final_color = texture(particle_texture, uv) * frag_color;
    }

c                 x    t           j        j                            t          dft
          dft          df          S )Nvertexgeometryfragment)pygletglcurrent_contextcreate_programvertex_sourcegeometry_sourcefragment_source     X/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/experimental/particles.pyget_default_shaderr      s9    9$33]H4M5Dj4Q5Dj4QS S Sr   c                  >     e Zd Zd fd	Zd Zd Zd Zd Zd Z xZ	S )	EmitterGroupNc                    t                                          |           || _        || _        || _        || _        d S )N)parent)super__init__texture	blend_src
blend_destprogram)selfr   r    r!   r"   r   	__class__s         r   r   zEmitterGroup.__init__   s>    '''"$r   c                   | j                                          t          t                     t	          | j        j        | j        j                   t          t                     t          | j        | j                   d S N)r"   useglActiveTextureGL_TEXTURE0glBindTexturer   targetidglEnableGL_BLENDglBlendFuncr    r!   r#   s    r   	set_statezEmitterGroup.set_state   se    $$$dl)4<?;;;DNDO44444r   c                `    t          t                     | j                                         d S r&   )	glDisabler.   r"   stopr0   s    r   unset_statezEmitterGroup.unset_state   s*    (r   c                0    | j         j         d| j         dS )N())r$   __name__r   r0   s    r   __repr__zEmitterGroup.__repr__   s     .);;DL;;;;r   c                   |j         | j         u oq| j        |j        u oc| j        |j        k    oS| j        j        |j        j        k    o9| j        j        |j        j        k    o| j        |j        k    o| j        |j        k    S r&   )r$   r"   r   r   r+   r,   r    r!   )r#   others     r   __eq__zEmitterGroup.__eq__   s    4>1 4-4u|+4 #u}';;4 5=#33	4
 %/14 5#33	5r   c                |    t          | j        | j        | j        j        | j        j        | j        | j        f          S r&   )hashr"   r   r   r,   r+   r    r!   r0   s    r   __hash__zEmitterGroup.__hash__   s8    T\4;\_dl&9^T_6 7 7 	7r   r&   )
r9   
__module____qualname__r   r1   r5   r:   r=   r@   __classcell__)r$   s   @r   r   r      s             5 5 5  < < <5 5 57 7 7 7 7 7 7r   r   c            	          e Zd ZdZdZdZdZdZdZdZ	e
Zddddeedddf	dZd Zed	             Zej        d
             Zd Zd Zd Zedd            Zej        dd            Zerd ZdS dS )EmitterNr   FT   rG   rG   rG         ?rI   c                   || _         || _        || _        || _        || _        ||z   | _        || _        |	| _        |
| _        || _	        t          |t          j                  rn|| _        |j        d         j                                        | _        |j        d         j        | _        | j        rt'          j        | j        | j                   n|                                | _        |pt-                      | _        |pt1          j                    | _        || _        |                     | j        ||| j        |          | _        |                                  d S )Nr   ) _img_x_y_z_count	_velocity_color_start
_color_end_scale_start
_scale_end
isinstancer   	Animation
_animationframesget_texture_textureduration_next_dtr   schedule_once_animater   _programr   get_default_batch_batch_user_groupgroup_classr"   _group_create_vertex_list)r#   imgxyzcountvelocityspreadcolor_start	color_endscale_start	scale_endr    r!   batchgroupr"   s                    r   r   zEmitter.__init__   s6    	!F*'#'#c5?++ 	.!DOJqM/;;==DMJqM2DM} B#DM4=AAAOO--DM7#5#7#7;x9;; &&t}iT\[`aa  """""r   c                   | j         }| j        }| j                            |t          | j        | j        d| j        | j        | j	        f|z  fd|j
        |j        |j        |j        f|z  fd| j        | j        z   |z  fd| j        |z  fd| j        |z  fd| j        |z  fd|j        |z  fd| j        f|z  fdt+          j                    f|z  f          | _        d S )NfBn)	positionsizescalerk   rm   rn   
texture_uvrotationbirth)rZ   rO   r"   vertex_list	GL_POINTSra   rd   rL   rM   rN   widthheightanchor_xanchor_yrS   rT   rP   rQ   rR   uv	_rotationtimeperf_counter_vertex_list)r#   r   rj   s      r   re   zEmitter._create_vertex_list
  s    - L449dk4;DGTWdg6>?w~w7GIYZ]bbc*T_<EF4>E12t0589T_u45WZ%/0DN,u45*,,.67 5 9 9r   c                    | j         S r&   )r_   r0   s    r   r"   zEmitter.program  s
    }r   c                \   | j         |k    rd S |                     | j        | j        j        | j        j        || j                  | _        | j        r.| j                            | j	        t          | j        |          rd S | j	                                         |                                  d S r&   )r_   rc   rZ   rd   r    r!   rb   ra   update_shaderr   r}   deletere   )r#   r"   s     r   r"   zEmitter.program!  s    =G##F&&t}'+{'<'+{'='.'+'7	9 9
 K 	))$*;YU\]]	 F 	  """  """""r   c                    | j         rt          j        | j                   | j                                         d| _        d| _        d| _        dS )zForce immediate removal of the emitter from video memory.

        This is often necessary when using batches, as the Python garbage
        collector will not necessarily call the finalizer immediately.
        N)rW   r   
unscheduler^   r   r   rZ   rd   r0   s    r   r   zEmitter.delete3  sP     ? 	,T]+++  """ r   c                "   | xj         dz  c_         | j         t          | j        j                  k    r%d| _         |                     d           | j        d S | j        j        | j                  }|                     |j                                                   |j	        X|j	        | j
        |z
  z
  }t          t          d|          |j	                  }t          j        | j        |           || _
        d S |                     d           d S )N   r   on_animation_endg        )_frame_indexlenrW   rX   dispatch_eventr   _set_texturer   rY   r[   r\   minmaxr   r]   r^   )r#   dtframer[   s       r   r^   zEmitter._animate@  s   QDO$: ; ;;; !D 2333 (&t'89%+1133444>%~);<H3sH--u~>>Hx888$DMMM 233333r   c                f   |j         | j        j         ur| j                            || j        j        | j        j        | j        j        | j        j                  | _        | j        	                                 || _        | 
                                 n|j        | j        j        d d <   || _        d S r&   )r,   rZ   rd   r$   r    r!   r"   r   r   r   re   r   ry   )r#   r   s     r   r   zEmitter._set_textureS  s    :T]---+//040E040F040C040B	D DDK
 $$&&&#DM$$&&&&.5jD(+r   return,tuple[int | float, int | float, int | float]c                *    | j         | j        | j        fS r&   )rL   rM   rN   r0   s    r   rv   zEmitter.positiona  s    w((r   rv   c                R    |\  | _         | _        | _        || j        j        d d <   d S r&   )rL   rM   rN   r   rv   )r#   rv   s     r   rv   zEmitter.positione  s.    $,!$'(0"111%%%r   c                    dS )a  The emitter animation reached the final frame.

            The event is triggered only if the emitter has an animation, not an
            image. For looping animations, the event is triggered each time
            the animation loops.

            :event:
            Nr   r0   s    r   r   zEmitter.on_animation_endk  s      r   )r   r   )rv   r   )r9   rA   rB   ra   rW   r   _pausedr   _visibler   r   rc   GL_SRC_ALPHAGL_ONE_MINUS_SRC_ALPHAr   re   propertyr"   setterr   r^   r   rv   _is_pyglet_doc_runr   r   r   r   rE   rE      sD       FJLGIHLK 2=Q':'4J4	# # # #B9 9 9&   X ^# # ^#"  4 4 4&      ) ) ) X) _1 1 1 _1  		 	 	 	 		 	r   rE   r   c                  2    e Zd Z	 	 	 	 d
dZd Zd Zdd	ZdS )ParticleManager      $@r   rF   rH   Nc                \   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        t                      | _        t          j        | j        d           d| _        t"          j                            ddddd|
          | _        d S )Ng?r   zparticles: 0
      )r      r   )dpicolorrq   )rK   	_lifespanrO   rP   _spreadrQ   rR   rS   rT   ra   rd   r   r_   r   schedule_interval_update_shader_timetotal_numberr   textLabeltotal_label)r#   rf   lifespanrj   rk   rl   rm   rn   ro   rp   rq   rr   s               r   r   zParticleManager.__init__{  s     	!!'#'#*,, 8&AAA !;,,^RTain,oor   c                <    t          j                    | j        d<   d S )Nr   )r   r   r_   )r#   r   s     r   r   z#ParticleManager._update_shader_time  s     $ 1 3 3fr   c                    |                                  | xj        dz  c_        d| j        | j        z  dz  | j        _        d S )Nr   particles:    )r   r   rO   r   r   )r#   r   emitters      r   _delete_callbackz ParticleManager._delete_callback  sQ     	Q Ud.?$+.MPQ.Q U Ur   r   c                V   t          | j        |||| j        | j        | j        | j        | j        | j        | j        | j	        | j
                  }t          j                            | j        | j        |           | xj        dz  c_        d| j        | j        z  dz  | j        _        |S )N)rm   rn   ro   rp   rq   rr   r   r   r   )rE   rK   rO   rP   r   rQ   rR   rS   rT   ra   rd   r   r   r]   r   r   r   r   r   )r#   rg   rh   ri   r   s        r   create_emitterzParticleManager.create_emitter  s    $)Q1dk4>4<&*&74?&*&74? $4;@ @ @ 	""4#8$.'RRR 	Q Ud.?$+.MPQ.Q U Ur   )r   rF   rF   rH   rH   NN)r   )r9   rA   rB   r   r   r   r   r   r   r   r   r   y  so         %=Q3=#'	p p p p24 4 4V V V     r   r   )
__future__r   sysr   r   r   r   r   r   	pyglet.glhasattrr	   r   r   r   r   r   Groupr   EventDispatcherrE   register_event_typer   r   r   r   <module>r      sU   # " " " " " 



   0 0 0 0 0 0 0 0 0 0 0 0    WS"566P3;P !FqfS S S$7 $7 $7 $7 $78> $7 $7 $7NT T T T Te# T T Tn   . / / /0 0 0 0 0 0 0 0 0 0r   