Exemple #1
0
        private void ConstructWalls()
        {
            // Connect the light controls to the walls
            LightControl1.AddTargetVisual(_sceneContainer);
            LightControl2.AddTargetVisual(_sceneContainer);

            LightControl1.CoordinateSpace = _worldSpaceContainer;
            LightControl2.CoordinateSpace = _worldSpaceContainer;

            // Setup the perspective transform
            UpdatePerspective();

            float boxWidth  = 6000.0f;
            float boxHeight = 2000.0f;
            float boxDepth  = 4000.0f;
            CompositionColorBrush colorBrush = _compositor.CreateColorBrush(Color.FromArgb(255, 101, 104, 112));

            // Create the walls
            SpriteVisual bottomWall = _compositor.CreateSpriteVisual();

            bottomWall.Size  = new Vector2(boxWidth, boxDepth);
            bottomWall.Brush = colorBrush;
            bottomWall.BackfaceVisibility = CompositionBackfaceVisibility.Hidden;
            bottomWall.TransformMatrix    = Matrix4x4.CreateRotationX((float)Math.PI / 2, new Vector3(boxWidth / 2, boxDepth / 2, 0));
            bottomWall.Offset             = new Vector3(-boxWidth / 2, -boxDepth / 2 + boxHeight / 2, 0);
            _sceneContainer.Children.InsertAtTop(bottomWall);

            SpriteVisual backWall = _compositor.CreateSpriteVisual();

            backWall.Size  = new Vector2(boxWidth, boxHeight);
            backWall.Brush = colorBrush;
            backWall.BackfaceVisibility = CompositionBackfaceVisibility.Hidden;
            backWall.Offset             = new Vector3(-boxWidth / 2, -boxHeight / 2, -boxDepth / 2);
            _sceneContainer.Children.InsertAtBottom(backWall);
        }
Exemple #2
0
        private async void SamplePage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            ConstructWalls();

            CompositionDrawingSurface normalMap = await SurfaceLoader.LoadFromUri(new Uri("ms-appx:///Samples/SDK 14393/LightSpheres/SphericalWithMask.png"));

            CompositionSurfaceBrush normalBrush = _compositor.CreateSurfaceBrush(normalMap);

            LightControl1.Offset                     = new Vector3(-77, 21, -768);
            LightControl1.LightColor.Color           = Color.FromArgb(255, 255, 255, 255);
            LightControl1.LinearAttenuation.Value    = 29;
            LightControl1.ConstantAttenuation.Value  = 50;
            LightControl2.QuadraticAttenuation.Value = 0;

            LightControl2.Offset                     = new Vector3(2047, 500, -676);
            LightControl2.LightColor.Color           = Color.FromArgb(255, 255, 0, 255);
            LightControl2.LinearAttenuation.Value    = 0;
            LightControl2.ConstantAttenuation.Value  = 9;
            LightControl2.QuadraticAttenuation.Value = 100;

            // Create the colors for the spheres
            Color[] colors = new Color[]
            {
                Color.FromArgb(255, 60, 60, 60),        // Black
                Color.FromArgb(255, 200, 200, 200),     // White
                Color.FromArgb(255, 139, 255, 211),     // Green
                Color.FromArgb(255, 160, 244, 255),     // Blue
                Color.FromArgb(255, 253, 99, 105),      // Red
                Color.FromArgb(255, 206, 181, 251),     // Purple
                Color.FromArgb(255, 67, 228, 124),      // Bright Green
                Color.FromArgb(255, 248, 216, 148),     // Yellow
                Color.FromArgb(255, 228, 190, 198),     // Mauve
            };

            // Create the lighting effect for the spheres
            IGraphicsEffect graphicsEffect = new CompositeEffect()
            {
                Mode    = CanvasComposite.DestinationIn,
                Sources =
                {
                    new ArithmeticCompositeEffect()
                    {
                        Source1Amount  = .9f,
                        Source2Amount  = 1f,
                        MultiplyAmount = 0,
                        Source1        = new ArithmeticCompositeEffect()
                        {
                            MultiplyAmount = .5f,
                            Source1Amount  = .5f,
                            Source2Amount  = 0f,
                            Source1        = new ArithmeticCompositeEffect()
                            {
                                MultiplyAmount = 1f,
                                Source1Amount  = 0f,
                                Source2Amount  = 0f,
                                Source1        = new ColorSourceEffect()
                                {
                                    Name = "Color",
                                },
                                Source2 = new SceneLightingEffect()
                                {
                                    Name            = "Ambient",
                                    AmbientAmount   = 1f,
                                    DiffuseAmount   = 0f,
                                    SpecularAmount  = 0f,
                                    NormalMapSource = new CompositionEffectSourceParameter("ImageSource"),
                                }
                            },
                            Source2 = new SceneLightingEffect()
                            {
                                Name            = "Diffuse",
                                AmbientAmount   = 0f,
                                DiffuseAmount   = 1f,
                                SpecularAmount  = 0f,
                                NormalMapSource = new CompositionEffectSourceParameter("ImageSource"),
                            }
                        },
                        Source2 = new SceneLightingEffect()
                        {
                            Name            = "Specular",
                            AmbientAmount   = 0,
                            DiffuseAmount   = 0f,
                            SpecularAmount  = .1f,
                            SpecularShine   = 5,
                            NormalMapSource = new CompositionEffectSourceParameter("ImageSource"),
                        }
                    },
                    new CompositionEffectSourceParameter("ImageSource"),
                }
            };

            CompositionEffectFactory effectFactory = _compositor.CreateEffectFactory(graphicsEffect,
                                                                                     new string[] { "Color.Color", "Diffuse.DiffuseAmount",
                                                                                                    "Specular.SpecularAmount", "Specular.SpecularShine" });

            // Create the spheres
            int xOffset = 0;

            foreach (Color color in colors)
            {
                CompositionEffectBrush brush = effectFactory.CreateBrush();
                brush.Properties.InsertColor("Color.Color", color);
                brush.SetSourceParameter("ImageSource", normalBrush);
                _brushList.Add(brush);

                SpriteVisual sprite = _compositor.CreateSpriteVisual();
                sprite.Brush  = brush;
                sprite.Size   = new Vector2(200, 200);
                sprite.Offset = new Vector3(xOffset, 0, 0) + _defaultSphereOffset;
                _sceneContainer.Children.InsertAtTop(sprite);
                _sphereList.Add(sprite);
                xOffset += _defaultSphereSpace;

                LightControl1.AddTargetVisual(sprite);
                LightControl2.AddTargetVisual(sprite);
            }

            UpdateAnimationState();
        }