### 技术控

今日:71| 主题:51389

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

aenji10 发表于 2016-10-1 12:03:48
162 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:
"#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:

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:

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:

wx_yQ4OPAAx 发表于 2016-10-2 14:55:09
 哥不说话，笑着路过！

 大人，此事必有蹊跷！

 我也是坐沙发的

 姐不是电视机，不要老是盯着姐看。

 aenji10的帖子香飘飘

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

 敢进男厕所的女汉子别隐身

wenziyo 发表于 2016-11-8 17:39:58
 为保住菊花，这个一定得回复！

• ## 王健林创业三年被告222次，刘强东34岁一夜

大佬的首次创业比你想象中要惨很多，那时候 [...]

• ## 清科数据：12月投资数量小幅上升 人工智能

12月19日，国务院关于印发“十三五”国家战略性 [...]

• ## CMU邢波教授主题演讲：医疗决策中的弊病给

有些人的血液里，天生流动着革命和创新的因子， [...]

• ## 2017支付宝五福卡有多少人集齐？集福套路都

前两天看新闻，有媒体称支付宝上已经有50万人集 [...]

• ## 支付宝集五福提示：对不起 您今天扫福字得

一年一度的集福卡的活动又开始了，可是像我这最 [...]

• ## 让你像吃薯片一样吃香菇，菇滋菇滋要做菌菇

毫无疑问，健康是零食的大趋势，传统零食如饼 [...]

• ## 我的天啊！怎么又缺少了 点击本文领取你的

2.5亿现金红包!腾讯QQ春节红包今日开抢 红包入口 [...]

• ## 2016化妆品零售总额2222亿 增速连年下滑

【订阅DT电商官方微信“DT电商网”，更多行业数 [...]

• ## 新成立公司数量不足去年四分之一？2016年中

一 　　成立时间：创业热度趋于冷静 　　 [...]

• ## 00后人群年终调查研究报告 小肚皮大数据最

2016年网友们是不是每天被网上的洪荒之力 [...]

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