win2d添加普通特效挺简单的,但是添加光效需要进行一些组合.
微软提供了 CompositeEffect API 提供使用.
但是最近比较烦,示例代码越看越烦,还是自己用win2d组合一下吧.
基本思路:
win2d的光效主要是分为
点光源 / 漫反射
光斑光源 / 镜面反射
当然还有其他的,这里我也没研究了.上面的特效连线一下主要分为
点光源漫反射--------PointDiffuse
点光源镜面反射--------PointSpecular
光斑漫反射--------SpotDiffuse
光斑镜面反射--------SpotDiffuse
然后像上面的四个还可以组合,比如 镜面点光源和光斑xx光源组合一下,这个在windows composition samples 里面展示了,可以下载看一下.上面的四种我只写了两种,因为懒
点光源镜面反射--------PointSpecular
光斑漫反射--------SpotDiffuse
选这两种是因为感觉效果有点代表性.下面是代码:
public async Task<CanvasImageSource> PointSpecularEffect() { imageSource = new CanvasImageSource(device, ImageWidth, ImageHeight, devicedpi); using (var readStream = await storageFile.OpenReadAsync()) { cbi = await CanvasBitmap.LoadAsync(device, readStream); } var heightMap = new LuminanceToAlphaEffect { Source = cbi, }; _pointSpecularEffect = new PointSpecularEffect { Source = heightMap, LightPosition = new Vector3(100, 100, 100), SpecularAmount = 0.9f, SpecularExponent = 50, }; _pointSpecularArithmetic = new ArithmeticCompositeEffect { Source1 = _pointSpecularEffect, Source1Amount=1, Source2 = cbi, Source2Amount=1, //MultiplyAmount=0, }; using (CanvasDrawingSession ds = imageSource.CreateDrawingSession(Colors.Transparent)) { ds.Clear(Colors.Transparent); ds.DrawImage(_pointSpecularArithmetic); } return imageSource; }
上面的代码用了三个特效组合:
1.镜面反射需要一个heightmap,
2.然后需要一个点光源光效特效,
3.最后合成特效
返回ImageSource直接可以赋值给Image控件的Source属性.
如果使用漫反射,就简单些,直接贴代码,不解释了.
这些纯win2d特效代码也不需要定义canvas控件.
----------------------------------
public async Task<CanvasImageSource> DrawSpotLightEffect() { imageSource = new CanvasImageSource(device, ImageWidth, ImageHeight, devicedpi); using (var readStream = await storageFile.OpenReadAsync()) { cbi = await CanvasBitmap.LoadAsync(device, readStream); //var heightMap = new LuminanceToAlphaEffect //{ // Source = cbi, //}; _spotDiffuseEffect = new SpotDiffuseEffect { Source =cbi, LightPosition = new Vector3(100, 100, 100), LightTarget = new Vector3(100, 100, 0), DiffuseAmount=0.4f, LimitingConeAngle = (float)Math.PI * 0.5f, }; _spotDiffuseArithmetic = new ArithmeticCompositeEffect { Source1 = cbi, Source2 = _spotDiffuseEffect, Source1Amount =1, Source2Amount=1, }; using (CanvasDrawingSession ds = imageSource.CreateDrawingSession(Colors.Transparent)) { ds.Clear(Colors.Transparent); ds.DrawImage(_spotDiffuseArithmetic); } } return imageSource; }
-----
其他单一特效就简单得很,掠过.想看效果的可以下载一下 这个app前往商店下载:链接 ,这里文末有限时免费代码,(限时免费结束还是可以免费试用),或者上面提到的微软官方示例windows composition samples.