技术控

    今日:136| 主题:49228
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Using MetalKit part 2*3^2

[复制链接]
aenji10 发表于 2016-10-1 12:03:48
155 8

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Yes, as the title suggests, we’re going to have another of those posts with math (and fun) in it. I was thinking the other day, what can we do while commuting for an hour or so, without internet and possibly without a laptop as well, just carrying an iPad with us. Luckily, the iPad now has the awesome Swift Playgrounds app.
   Let’s start with a new playground that runs a basic compute kernel. Since the current version of the Swift Playgrounds app does not yet let us edit the Auxiliary Source Files , where all our Swift and Metal files usually reside, we will have to write our code in the main playground page, but it is not that complicated. All we have to do is modify our MetalView initializer and let it take in an extra argument – our shader/kernel code. Then we start building our code by adding more lines to this long string.
  Let’s start with a light blue sky background color:
     [code]let shader =
"#include \n" +
"using namespace metal;" +
"kernel void k(texture2d o[[texture(0)]]," +
"              uint2 gid[[thread_position_in_grid]]) {" +
"   float3 color = float3(0.5, 0.8, 1.0);" +
"   o.write(float4(color, 1.0), gid);" +
"}"[/code]    If you run the playground now, the output image should look like this:
   
Using MetalKit part 2*3^2-1 (background,internet,building,carrying,possibly)

   Next, let’s draw a gradient. we divide the current pixel coordinates to the screen dimensions and we get UV – a pair of floats between (0-1) . we then multiply the fixed color with Y – the vertical component of UV which gives us the gradient:
     [code]"   int width = o.get_width();" +
"   int height = o.get_height();" +
"   float2 uv = float2(gid) / float2(width, height);" +
"   color *= uv.y;" +[/code]    The output image should look like this:

Using MetalKit part 2*3^2-2 (background,internet,building,carrying,possibly)

   Let’s work on a nicer background next. A smooth gradient would look like a great sunset. We can use mix to blend colors. We tell the function to blend vertically, and take the complement of Y to switch the colors:
     [code]"   float3 color = mix(float3(1.0, 0.6, 0.1), float3(0.5, 0.8, 1.0), sqrt(1 - uv.y));" +[/code]    The output image should look like this:
友荐云推荐




上一篇:ggplot2 2.2.0 coming soon!
下一篇:redis配置文件参数说明及命令操作
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

wx_yQ4OPAAx 发表于 2016-10-2 14:55:09
哥不说话,笑着路过!
回复 支持 反对

使用道具 举报

千雁 发表于 2016-10-2 21:18:32
大人,此事必有蹊跷!
回复 支持 反对

使用道具 举报

景艳 发表于 2016-10-6 09:53:15
我也是坐沙发的
回复 支持 反对

使用道具 举报

黄荣 发表于 2016-10-9 10:57:23
姐不是电视机,不要老是盯着姐看。
回复 支持 反对

使用道具 举报

悲伤述说微笑 发表于 2016-10-9 11:20:06
aenji10的帖子香飘飘
回复 支持 反对

使用道具 举报

じ★ve梦悱灬 发表于 2016-10-11 15:50:43
不回帖的话就太任性了
回复 支持 反对

使用道具 举报

堇年纸鸢 发表于 2016-10-15 10:39:07
敢进男厕所的女汉子别隐身
回复 支持 反对

使用道具 举报

wenziyo 发表于 2016-11-8 17:39:58
为保住菊花,这个一定得回复!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表