1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use bevy::
{
    asset::{Handle, Assets},
    app::{Plugin, AppBuilder},
    render::
    {
        mesh::Mesh,
        entity::MeshBundle,
        pipeline::{RenderPipelines, PipelineDescriptor, RenderPipeline},
        shader::{Shader, ShaderStages, ShaderStage},
    },
    ecs::
    {
        entity::Entity,
        system::{IntoSystem, Commands, ResMut, Query, Res},
        query::{With, Added},
        // schedule::ExclusiveSystemDescriptorCoercion,
    },
    core::Time,
};

use crate::play::ScheduledAnimation;
use crate::core::{PackedAnimation, AnimationKind};
use crate::draw::Drawing;
use std::time::Duration;

const VERTEX_SHADER: &str = r#"
#version 450

layout(location = 0) in vec3 Vertex_Position;
layout(location = 1) in vec4 Vertex_Color;
layout(location = 0) out vec4 v_color;

layout(set = 0, binding = 0) uniform CameraViewProj {
    mat4 ViewProj;
};

layout(set = 1, binding = 0) uniform Transform {
    mat4 Model;
};

void main() {
    gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0);
    v_color = Vertex_Color;
}
"#;

const FRAGMENT_SHADER: &str = r#"
#version 450

layout(location = 0) in vec4 v_color;
layout(location = 0) out vec4 o_Target;

void main() {
    o_Target = v_color;
}
"#;

pub struct ManimRenderPipelines
{
    pub pipelines: RenderPipelines
}


pub struct ManimPlugin;
impl Plugin for ManimPlugin
{
    fn build(&self, app: &mut AppBuilder)
    {
        app.add_plugin(registration::RegisterSystems);
        app.add_system(animation_processor.system());

        let world = app.world_mut();
        let mut shaders = world.get_resource_mut::<Assets<Shader>>().unwrap();
        let pipeline = PipelineDescriptor::default_config(ShaderStages {
            vertex: shaders.add(Shader::from_glsl(ShaderStage::Vertex, VERTEX_SHADER)),
            fragment: Some(shaders.add(Shader::from_glsl(ShaderStage::Fragment, FRAGMENT_SHADER))),
        });
        drop(shaders);
        let mut pipelines = world.get_resource_mut::<Assets<PipelineDescriptor>>().unwrap();
        let pipeline_handle = pipelines.add(pipeline);
        let render_pipelines = RenderPipelines::from_pipelines(vec![RenderPipeline::new(
            pipeline_handle,
        )]);
        world.insert_resource(ManimRenderPipelines { pipelines: render_pipelines })
    }
}

pub struct ManimDrawing;

fn animation_processor
(
    mut commands: Commands,
    mut animated: Query<(Entity, &Handle<Mesh>, &mut ScheduledAnimation), With<ManimDrawing>>,
    mut meshes: ResMut<Assets<Mesh>>,
    time: Res<Time>,
)
{
    let since_start = time.seconds_since_startup() as f32;
    for (entity, mesh_handle, mut scheduled_animation) in animated.iter_mut()
    {
        if let Some(mesh) = meshes.get_mut(mesh_handle.clone())
        {
            let progress = (since_start - scheduled_animation.start) / scheduled_animation.duration.as_secs_f32();
            if progress <= 0.0
            {
                configure_mesh(mesh, &scheduled_animation.animation, 0.0);
            }
            else if progress > 0.0 && progress < 1.0
            {
                configure_mesh(mesh, &scheduled_animation.animation, progress);
            }
            else if progress >= 1.0
            {
                if scheduled_animation.is_loop
                {
                    scheduled_animation.start = since_start
                }
                else
                {
                    commands.entity(entity).remove::<ScheduledAnimation>();
                }
            }
        }
        else
        {
            commands.entity(entity).remove::<ScheduledAnimation>();
        }
    }
}

fn configure_mesh(mesh: &mut Mesh, animation: &PackedAnimation, progress: f32)
{
    let mut tmp = animation.lock().unwrap();

    // this line causes lag
    // if tmp.get_progress() == progress { return }

    tmp.set_progress(progress);
    mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, tmp.get_vertices());
    mesh.set_attribute(Mesh::ATTRIBUTE_COLOR, tmp.get_colors());
    mesh.set_indices(Some(tmp.get_indices()));
}

pub struct AnimationDescription<A: AnimationKind>
{
    pub animation: A,
    pub start: f32,
    pub duration: Duration,
    pub is_loop: bool,
}

pub(crate) fn register_drawing<T: Drawing>
(
    mut commands: Commands,
    query: Query<(Entity, &T), Added<T>>,
    mut meshes: ResMut<Assets<Mesh>>,
    pipeline: Res<ManimRenderPipelines>
)
{
    for (entity, drawing) in query.iter()
    {
        let mesh = drawing.get_mesh();
        let handle = meshes.add(mesh);
        commands.entity(entity)
            .insert(ManimDrawing)
            .insert_bundle(MeshBundle
            {
                mesh: handle,
                render_pipelines: pipeline.pipelines.clone(),
                ..Default::default()
            });
    }
}

pub(crate) fn register_animation<D: Drawing, A: AnimationKind>
(
    mut commands: Commands,
    query: Query<(Entity, &D, &AnimationDescription<A>), Added<AnimationDescription<A>>>
)
{
    for (entity, drawing, animation) in query.iter()
    {
        commands.entity(entity)
            .remove::<AnimationDescription<A>>()
            .insert(ScheduledAnimation
            {
                is_loop: animation.is_loop,
                start: animation.start,
                duration: animation.duration,
                animation: drawing.animate(animation.animation.clone())
            });
    }
}


mod registration
{
    use bevy::app::{Plugin, AppBuilder, CoreStage};
    use bevy::ecs::system::IntoSystem;
    use crate::bevy::register_animation;
    use crate::bevy::register_drawing;
    use crate::draw::primitives::*;
    use crate::anim::emergence::*;

    macro_rules! nested_register {
        ($app:expr; ($($draw:ident),*) $anim:tt) => {
            $(nested_register!(@register $app; $draw $anim);)*
        };
        (@register $app:expr; $draw:ident ($($anim:ident),*)) => {
            $app.add_system_to_stage(CoreStage::PostUpdate, register_drawing::<$draw>.system());
            $($app.add_system_to_stage(CoreStage::Last, register_animation::<$draw, $anim>.system());)*
        };
    }

    pub(crate) struct RegisterSystems;
    impl Plugin for RegisterSystems
    {
        fn build(&self, app: &mut AppBuilder)
        {
            nested_register!(
            app;
            (Line, Point, Rect, Polygon, Circle)
            (Fade, FromPoint)
            );
        }
    }
}