Exemplo n.º 1
0
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var compositor = Window.Current.Compositor;

            _surface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///Assets/webster-pass.png"));

            var backgroundBrush = compositor.CreateSurfaceBrush(_surface);

            backgroundBrush.Stretch = CompositionStretch.UniformToFill;

            var saturationEffect = new SaturationEffect
            {
                Saturation = 0.0f,
                Source     = new CompositionEffectSourceParameter("mySource")
            };

            var saturationEffectFactory = compositor.CreateEffectFactory(saturationEffect);

            var bwEffect = saturationEffectFactory.CreateBrush();

            bwEffect.SetSourceParameter("mySource", backgroundBrush);

            _backgroundVisual       = compositor.CreateSpriteVisual();
            _backgroundVisual.Brush = bwEffect;
            _backgroundVisual.Size  = RootElement.RenderSize.ToVector2();


            _containerVisual = compositor.CreateContainerVisual();
            _containerVisual.Children.InsertAtBottom(_backgroundVisual);
            ElementCompositionPreview.SetElementChildVisual(RootElement, _containerVisual);

            // Text
            _surfaceFactory = SurfaceFactory.GetSharedSurfaceFactoryForCompositor(compositor);

            _textSurface = _surfaceFactory.CreateTextSurface("Weston Pass");
            _textSurface.ForegroundColor = Color.FromArgb(50, 255, 255, 255);
            _textSurface.FontSize        = 150;
            var textSurfaceBrush = compositor.CreateSurfaceBrush(_textSurface.Surface);


            _textVisual      = compositor.CreateSpriteVisual();
            _textVisual.Size = _textSurface.Size.ToVector2();
            _textVisual.RotationAngleInDegrees = 45f;
            _textVisual.AnchorPoint            = new Vector2(0.5f);

            _textVisual.Brush = textSurfaceBrush;
            _textVisual.StartAnimation(nameof(Visual.Offset), CreateTextOffsetAnimation(
                                           new Vector3((float)RootElement.ActualWidth / 2, (float)RootElement.ActualWidth / 2, 0)));

            _containerVisual.Children.InsertAtTop(_textVisual);

            AddLighting();

            StartLightingAnimationTimer();
        }
Exemplo n.º 2
0
        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            // Compositor
            _compositor = Window.Current.Compositor;

            // Load Image
            _imageSurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///Assets/webster-pass.png"));
            var backgroundBrush = _compositor.CreateSurfaceBrush(_imageSurface);

            backgroundBrush.Stretch = CompositionStretch.UniformToFill;

            // Apply Saturation Effect (Win2D)
            var saturationEffect = new SaturationEffect
            {
                Saturation = 0,
                Source     = new CompositionEffectSourceParameter("background")
            };

            var saturationEffectFactory = _compositor.CreateEffectFactory(saturationEffect);
            var saturationBrush         = saturationEffectFactory.CreateBrush();

            saturationBrush.SetSourceParameter("background", backgroundBrush);

            // Add background to visual tree
            _backgroundVisual       = _compositor.CreateSpriteVisual();
            _backgroundVisual.Brush = saturationBrush;
            _backgroundVisual.Size  = RootElement.RenderSize.ToVector2();

            _containerVisual = _compositor.CreateContainerVisual();
            _containerVisual.Children.InsertAtBottom(_backgroundVisual);
            ElementCompositionPreview.SetElementChildVisual(RootElement, _containerVisual);

            // Add Lighting
            _ambientLight           = _compositor.CreateAmbientLight();
            _ambientLight.Intensity = 1.5f;
            _ambientLight.Color     = Colors.Purple;
            _ambientLight.Targets.Add(_backgroundVisual);

            _pointLight1                 = _compositor.CreatePointLight();
            _pointLight1.Color           = Colors.Yellow;
            _pointLight1.CoordinateSpace = _containerVisual;
            _pointLight1.Targets.Add(_backgroundVisual);
            _pointLight1.Offset = new Vector3((float)RootElement.ActualWidth, (float)RootElement.ActualHeight * 0.25f, 300);

            _pointLight2                 = _compositor.CreatePointLight();
            _pointLight2.Color           = Colors.Green;
            _pointLight2.Intensity       = 2f;
            _pointLight2.CoordinateSpace = _containerVisual;
            _pointLight2.Targets.Add(_backgroundVisual);
            _pointLight2.Offset = new Vector3(0, (float)RootElement.ActualHeight * 0.75f, 300);

            // Animate Lights - Create expression template
            var offsetAnimation = Window.Current.Compositor.CreateVector3KeyFrameAnimation();

            offsetAnimation.Target = nameof(PointLight.Offset);
            offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
            offsetAnimation.Duration = TimeSpan.FromSeconds(10);

            // Animate Lights - create implicit animations
            _implicitOffsetAnimation = _compositor.CreateImplicitAnimationCollection();
            _implicitOffsetAnimation[nameof(Visual.Offset)] = offsetAnimation;

            _pointLight1.ImplicitAnimations = _implicitOffsetAnimation;
            _pointLight2.ImplicitAnimations = _implicitOffsetAnimation;

            // Animate Lights - create timer
            _lightTimer          = new DispatcherTimer();
            _lightTimer.Interval = TimeSpan.FromSeconds(10);
            _lightTimer.Tick    += LightTimerOnTick;
            _lightTimer.Start();

            // AnimateLights - initial move
            MoveLights();

            // Add Text (RobMikh.SurfaceFactory) Ø
            _surfaceFactory = SurfaceFactory.GetSharedSurfaceFactoryForCompositor(_compositor);
            _textSurface    = _surfaceFactory.CreateTextSurface("Hello Øredev!");
            _textSurface.ForegroundColor = Color.FromArgb(50, 255, 255, 255);
            _textSurface.FontSize        = 150;
            var textBrush = _compositor.CreateSurfaceBrush(_textSurface.Surface);

            _textVisual      = _compositor.CreateSpriteVisual();
            _textVisual.Size = _textSurface.Size.ToVector2();
            _textVisual.RotationAngleInDegrees = 45f;
            _textVisual.AnchorPoint            = new Vector2(0.5f);
            _textVisual.Brush = textBrush;
            _textVisual.StartAnimation(nameof(Visual.Offset), CreateTextAnimation());

            _containerVisual.Children.InsertAtTop(_textVisual);
        }