Esempio n. 1
0
        public void InitAnimations(SceneContainer sc)
        {
            _animation = new Animation();

            foreach (AnimationComponent ac in sc.Children.FindComponents <AnimationComponent>(c => true))
            {
                if (ac.AnimationTracks != null)
                {
                    foreach (AnimationTrackContainer animTrackContainer in ac.AnimationTracks)
                    {
                        Type t = animTrackContainer.KeyType;
                        if (typeof(int).IsAssignableFrom(t))
                        {
                            Channel <int> channel = new Channel <int>(Lerp.IntLerp);
                            foreach (AnimationKeyContainerInt key in animTrackContainer.KeyFrames)
                            {
                                channel.AddKeyframe(new Keyframe <int>(key.Time, key.Value));
                            }
                            _animation.AddAnimation(channel, animTrackContainer.SceneComponent,
                                                    animTrackContainer.Property);
                        }
                        else if (typeof(float).IsAssignableFrom(t))
                        {
                            Channel <float> channel = new Channel <float>(Lerp.FloatLerp);
                            foreach (AnimationKeyContainerFloat key in animTrackContainer.KeyFrames)
                            {
                                channel.AddKeyframe(new Keyframe <float>(key.Time, key.Value));
                            }
                            _animation.AddAnimation(channel, animTrackContainer.SceneComponent,
                                                    animTrackContainer.Property);
                        }
                        else if (typeof(float2).IsAssignableFrom(t))
                        {
                            Channel <float2> channel = new Channel <float2>(Lerp.Float2Lerp);
                            foreach (AnimationKeyContainerFloat2 key in animTrackContainer.KeyFrames)
                            {
                                channel.AddKeyframe(new Keyframe <float2>(key.Time, key.Value));
                            }
                            _animation.AddAnimation(channel, animTrackContainer.SceneComponent,
                                                    animTrackContainer.Property);
                        }
                        else if (typeof(float3).IsAssignableFrom(t))
                        {
                            Channel <float3> .LerpFunc lerpFunc;
                            switch (animTrackContainer.LerpType)
                            {
                            case LerpType.Lerp:
                                lerpFunc = Lerp.Float3Lerp;
                                break;

                            case LerpType.Slerp:
                                lerpFunc = Lerp.Float3QuaternionSlerp;
                                break;

                            default:
                                // C# 6throw new InvalidEnumArgumentException(nameof(animTrackContainer.LerpType), (int)animTrackContainer.LerpType, typeof(LerpType));
                                // throw new InvalidEnumArgumentException("animTrackContainer.LerpType", (int)animTrackContainer.LerpType, typeof(LerpType));
                                throw new InvalidOperationException("Unknown lerp type: animTrackContainer.LerpType: " + (int)animTrackContainer.LerpType);
                            }
                            Channel <float3> channel = new Channel <float3>(lerpFunc);
                            foreach (AnimationKeyContainerFloat3 key in animTrackContainer.KeyFrames)
                            {
                                channel.AddKeyframe(new Keyframe <float3>(key.Time, key.Value));
                            }
                            _animation.AddAnimation(channel, animTrackContainer.SceneComponent,
                                                    animTrackContainer.Property);
                        }
                        else if (typeof(float4).IsAssignableFrom(t))
                        {
                            Channel <float4> channel = new Channel <float4>(Lerp.Float4Lerp);
                            foreach (AnimationKeyContainerFloat4 key in animTrackContainer.KeyFrames)
                            {
                                channel.AddKeyframe(new Keyframe <float4>(key.Time, key.Value));
                            }
                            _animation.AddAnimation(channel, animTrackContainer.SceneComponent,
                                                    animTrackContainer.Property);
                        }
                        //TODO : Add cases for each type
                    }
                }
            }
        }
Esempio n. 2
0
        public override void Init()
        {
            _sceneManager = new SceneManager();
            _sceneManager.AttachToContext(RC);

            var dir = new DirectionalLight(new float3(0, 10, -1), new float4(1, 1, 1, 1), new float4(1, 1, 1, 1),
                                           new float4(1, 1, 1, 1), new float3(0, 0, 0), 0);

            var stativ = new SceneEntity("stativ", new ActionCode())
            {
                Transform =
                {
                    GlobalPosition = new float3(0, 0, 100)
                }
            };

            stativ.AddComponent(dir);

            _sceneManager.AddSceneEntity(stativ);

            _camera = new Camera(stativ);
            _camera.Resize(Width, Height);

            Geometry sohGeo = MeshReader.LoadGeometry("Assets/Sphere.obj.model");

            _sphere = new SceneEntity("sphere", new SphereMaterial(MoreShaders.GetDiffuseColorShader(RC)),
                                      new Renderer(sohGeo));

            _sceneManager.AddSceneEntity(_sphere);

            _channel2 = new Channel <float3>(Lerp.Float3Lerp);
            _channel1 = new Channel <float4>(Lerp.Float4Lerp, new float4(0.5f, 0.5f, 0.5f, 0.5f));

            var key0 = new Keyframe <float4>(0, new float4(1, 0, 1, 1));
            var key1 = new Keyframe <float4>(2, new float4(0.125f, 1, 0.125f, 1));
            var key2 = new Keyframe <float4>(4, new float4(0.250f, 0.75f, 0.250f, 1));
            var key3 = new Keyframe <float4>(6, new float4(0.5f, 0.5f, 0.5f, 1));
            var key4 = new Keyframe <float4>(8, new float4(0.75f, 0.25f, 0.75f, 1));
            var key5 = new Keyframe <float4>(10, new float4(1, 25, 0.125f, 1));
            var key6 = new Keyframe <float4>(0, new float4(0, 1, 0, 1));

            _channel1.AddKeyframe(key0);
            _channel1.AddKeyframe(key1);
            _channel1.AddKeyframe(key2);
            _channel1.AddKeyframe(key3);
            _channel1.AddKeyframe(key4);
            _channel1.AddKeyframe(key5);
            _channel1.AddKeyframe(key6);

            var key40 = new Keyframe <float3>(8, new float3(8, 0, 80));
            var key00 = new Keyframe <float3>(0, new float3(0, 0, 0));
            var key10 = new Keyframe <float3>(2, new float3(1, 2, 20));
            var key20 = new Keyframe <float3>(4, new float3(2, 4, 40));
            var key30 = new Keyframe <float3>(6, new float3(4, 4, 60));
            var key50 = new Keyframe <float3>(12, new float3(0, 4, 60));
            var key60 = new Keyframe <float3>(0, new float3(8, 8, 8));

            _channel2.AddKeyframe(key00);
            _channel2.AddKeyframe(key10);
            _channel2.AddKeyframe(key20);
            _channel2.AddKeyframe(key30);
            _channel2.AddKeyframe(key40);
            _channel2.AddKeyframe(key50);
            _channel2.AddKeyframe(key60);

            _myAnim.AddAnimation(_channel1, RC, "ClearColor");
            _myAnim.AddAnimation(_channel2, _sphere, "Transform.GlobalPosition");
        }