示例#1
0
        private ICanvasImage CreateLighting()
        {
            var heightMap = new LuminanceToAlphaEffect
            {
                Source = bitmapTiger
            };

            var distantDiffuseEffect = new DistantDiffuseEffect
            {
                Source         = heightMap,
                HeightMapScale = 2
            };

            var distantSpecularEffect = new DistantSpecularEffect
            {
                Source           = heightMap,
                SpecularExponent = 16
            };

            var pointDiffuseEffect = new PointDiffuseEffect
            {
                Source         = heightMap,
                HeightMapScale = 2
            };

            var pointSpecularEffect = new PointSpecularEffect
            {
                Source           = heightMap,
                SpecularExponent = 16
            };

            var spotDiffuseEffect = new SpotDiffuseEffect
            {
                Source            = heightMap,
                HeightMapScale    = 2,
                LimitingConeAngle = 0.25f,
                LightTarget       = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2
            };

            var spotSpecularEffect = new SpotSpecularEffect
            {
                Source            = heightMap,
                SpecularExponent  = 16,
                LimitingConeAngle = 0.25f,
                LightTarget       = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2
            };

            // Lay out all the different light types in a grid.
            var xgap = (float)bitmapTiger.Size.Width * 1.1f;
            var ygap = (float)bitmapTiger.Size.Height * 1.1f;

            var compositeEffect = new CompositeEffect
            {
                Sources =
                {
                    AddTextOverlay(distantDiffuseEffect,                            -xgap, -ygap),
                    AddTextOverlay(distantSpecularEffect,                           -xgap,     0),
                    AddTextOverlay(pointDiffuseEffect,                                 -0, -ygap),
                    AddTextOverlay(pointSpecularEffect,                                -0,     0),
                    AddTextOverlay(spotDiffuseEffect,               xgap,                  -ygap),
                    AddTextOverlay(spotSpecularEffect,              xgap,                      0),

                    CombineDiffuseAndSpecular(distantDiffuseEffect, distantSpecularEffect,  -xgap,ygap),
                    CombineDiffuseAndSpecular(pointDiffuseEffect,   pointSpecularEffect,        0,ygap),
                    CombineDiffuseAndSpecular(spotDiffuseEffect,    spotSpecularEffect,    xgap,  ygap),
                }
            };

            ICanvasImage finalEffect = compositeEffect;

            // Check the screen size, and scale down our output if the screen is too small to fit the whole thing as-is.
            var xScaleFactor = (float)canvas.ActualWidth / 750;
            var yScaleFactor = (float)canvas.ActualHeight / 500;

            var scaleFactor = Math.Min(xScaleFactor, yScaleFactor);

            if (scaleFactor < 1)
            {
                finalEffect = new Transform2DEffect
                {
                    Source          = compositeEffect,
                    TransformMatrix = Matrix3x2.CreateScale(scaleFactor, bitmapTiger.Size.ToVector2() / 2)
                };
            }

            // Animation changes the light directions.
            animationFunction = elapsedTime =>
            {
                distantDiffuseEffect.Azimuth      =
                    distantSpecularEffect.Azimuth = elapsedTime % ((float)Math.PI * 2);

                distantDiffuseEffect.Elevation      =
                    distantSpecularEffect.Elevation = (float)Math.PI / 4 + (float)Math.Sin(elapsedTime / 2) * (float)Math.PI / 8;

                pointDiffuseEffect.LightPosition             =
                    pointSpecularEffect.LightPosition        =
                        spotDiffuseEffect.LightPosition      =
                            spotSpecularEffect.LightPosition = new Vector3((float)Math.Cos(elapsedTime), (float)Math.Sin(elapsedTime), 1) * 100;
            };

            return(finalEffect);
        }
示例#2
0
        private ICanvasImage CreateLighting()
        {
            var heightMap = new LuminanceToAlphaEffect
            {
                Source = bitmapTiger
            };

            var distantDiffuseEffect = new DistantDiffuseEffect
            {
                Source = heightMap,
                HeightMapScale = 2
            };

            var distantSpecularEffect = new DistantSpecularEffect
            {
                Source = heightMap,
                SpecularExponent = 16
            };

            var pointDiffuseEffect = new PointDiffuseEffect
            {
                Source = heightMap,
                HeightMapScale = 2
            };

            var pointSpecularEffect = new PointSpecularEffect
            {
                Source = heightMap,
                SpecularExponent = 16
            };

            var spotDiffuseEffect = new SpotDiffuseEffect
            {
                Source = heightMap,
                HeightMapScale = 2,
                LimitingConeAngle = 0.25f,
                LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2
            };

            var spotSpecularEffect = new SpotSpecularEffect
            {
                Source = heightMap,
                SpecularExponent = 16,
                LimitingConeAngle = 0.25f,
                LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2
            };

            // Lay out all the different light types in a grid.
            var xgap = (float)bitmapTiger.Size.Width * 1.1f;
            var ygap = (float)bitmapTiger.Size.Height * 1.1f;

            var compositeEffect = new CompositeEffect
            {
                Sources =
                {
                    AddTextOverlay(distantDiffuseEffect,  -xgap, -ygap),
                    AddTextOverlay(distantSpecularEffect, -xgap,  0),
                    AddTextOverlay(pointDiffuseEffect,    -0,    -ygap),
                    AddTextOverlay(pointSpecularEffect,   -0,     0),
                    AddTextOverlay(spotDiffuseEffect,      xgap, -ygap),
                    AddTextOverlay(spotSpecularEffect,     xgap,  0),

                    CombineDiffuseAndSpecular(distantDiffuseEffect, distantSpecularEffect, -xgap, ygap),
                    CombineDiffuseAndSpecular(pointDiffuseEffect,   pointSpecularEffect,    0,    ygap),
                    CombineDiffuseAndSpecular(spotDiffuseEffect,    spotSpecularEffect,     xgap, ygap),
                }
            };

            ICanvasImage finalEffect = compositeEffect;

            // Check the screen size, and scale down our output if the screen is too small to fit the whole thing as-is.
            var xScaleFactor = (float)canvas.ActualWidth / 750;
            var yScaleFactor = (float)canvas.ActualHeight / 500;

            var scaleFactor = Math.Min(xScaleFactor, yScaleFactor);

            if (scaleFactor < 1)
            {
                finalEffect = new Transform2DEffect
                {
                    Source = compositeEffect,
                    TransformMatrix = Matrix3x2.CreateScale(scaleFactor, bitmapTiger.Size.ToVector2() / 2)
                };
            }

            // Animation changes the light directions.
            animationFunction = elapsedTime =>
            {
                distantDiffuseEffect.Azimuth = 
                distantSpecularEffect.Azimuth = elapsedTime % ((float)Math.PI * 2);

                distantDiffuseEffect.Elevation =
                distantSpecularEffect.Elevation = (float)Math.PI / 4 + (float)Math.Sin(elapsedTime / 2) * (float)Math.PI / 8;

                pointDiffuseEffect.LightPosition = 
                pointSpecularEffect.LightPosition =
                spotDiffuseEffect.LightPosition =
                spotSpecularEffect.LightPosition = new Vector3((float)Math.Cos(elapsedTime), (float)Math.Sin(elapsedTime), 1) * 100;
            };

            return finalEffect;
        }
        public CanvasRenderTarget apply3DLightingEffects(CanvasBitmap workingBitmap)
        {
            //CanvasBitmap workingBitmap = SelectWorkingBitmap(useOriginalBitmap);

            if (workingBitmap != null)
            {
                int ww = (int)workingBitmap.SizeInPixels.Width;
                int hh = (int)workingBitmap.SizeInPixels.Height;

                //LuminanceToAlphaEffect heightField = new LuminanceToAlphaEffect();
                //heightField.Source = workingBitmap;


                LuminanceToAlphaEffect heightMap = new LuminanceToAlphaEffect();
                heightMap.Source = workingBitmap;

                GaussianBlurEffect heightField = new GaussianBlurEffect();
                heightField.BlurAmount = (float)gaussianBlurAmount;
                heightField.Source     = heightMap;
                heightField.BorderMode = EffectBorderMode.Soft;

                DistantDiffuseEffect distantDiffuseEffect = new DistantDiffuseEffect();
                distantDiffuseEffect.Source                     = heightField;
                distantDiffuseEffect.HeightMapScale             = (float)distantDiffuseEffectHeightMapScale;
                distantDiffuseEffect.HeightMapInterpolationMode = CanvasImageInterpolation.HighQualityCubic;
                distantDiffuseEffect.Azimuth                    = (float)distantAzimuth;
                distantDiffuseEffect.Elevation                  = (float)distantElevation;
                distantDiffuseEffect.LightColor                 = distantLightColor;
                distantDiffuseEffect.HeightMapKernelSize        = new System.Numerics.Vector2((float)distantDiffuseKernelWidth, (float)distantDiffuseKernelWidth);
                //distantDiffuseEffect.DiffuseAmount = (float) distantDiffuseAmount;

                DistantSpecularEffect distantSpecularEffect = new DistantSpecularEffect();
                distantSpecularEffect.Source                     = heightField;
                distantSpecularEffect.SpecularExponent           = (float)distantSpecularEffectSpecularExponent;
                distantSpecularEffect.HeightMapInterpolationMode = CanvasImageInterpolation.HighQualityCubic;
                distantSpecularEffect.HeightMapKernelSize        = new System.Numerics.Vector2((float)distantSpecularKernelWidth, (float)distantSpecularKernelWidth);
                distantSpecularEffect.HeightMapScale             = (float)distantSpecularHeightMapScale;
                distantSpecularEffect.Azimuth                    = (float)distantAzimuth;
                distantSpecularEffect.Elevation                  = (float)distantElevation;
                //distantSpecularEffect.SpecularAmount = (float)distantSpecularAmount;
                //distantSpecularEffect.LightColor = distantLightColor;

                ArithmeticCompositeEffect arithmeticCompositeEffect = new ArithmeticCompositeEffect();
                arithmeticCompositeEffect.Source1 = distantDiffuseEffect;
                //arithmeticCompositeEffect.Source1 = blendedDiffuseEffect;
                arithmeticCompositeEffect.Source2        = distantSpecularEffect;
                arithmeticCompositeEffect.Source1Amount  = 1;
                arithmeticCompositeEffect.Source2Amount  = 1;
                arithmeticCompositeEffect.MultiplyAmount = 0;

                SaturationEffect saturationEffect = new SaturationEffect();
                saturationEffect.Source     = workingBitmap;
                saturationEffect.Saturation = (float)distantDiffuseSaturation;

                ArithmeticCompositeEffect blendedDiffuseEffect = new ArithmeticCompositeEffect();
                //blendedDiffuseEffect.Source1 = workingBitmap;
                blendedDiffuseEffect.Source1        = saturationEffect;
                blendedDiffuseEffect.Source2        = arithmeticCompositeEffect;
                blendedDiffuseEffect.Source1Amount  = 0;
                blendedDiffuseEffect.Source2Amount  = 0;
                blendedDiffuseEffect.MultiplyAmount = 1;


                //if (canvasRenderTarget != null)
                //    canvasRenderTarget.Dispose();
                CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), ww, hh, canvasBitmap.Dpi);
                using (var session = canvasRenderTarget.CreateDrawingSession())
                {
                    //session.DrawImage(arithmeticCompositeEffect);
                    session.DrawImage(blendedDiffuseEffect);
                }

                return(canvasRenderTarget);
            }

            return(null);
        }