本系列文章是对 metalkit.org 上面MetalKit内容的全面翻译和学习.git
MetalKit系统文章目录github
今天咱们从Peter Shirley’s mini book导入ray tracer射线追踪器
到Swift
playground中.我将不会解释什么是Ray Tracing射线追踪及它是怎么工做的,我会请你本身先去读一读这本书由于它对Kindle订阅者是免费的.若是你不是订阅者,只须要像我同样购买这本书就好了.若是你对这个话题感兴趣,那花费$2.99是绝对值得的.swift
咱们要作的第一件事就是建立一个数据结构体来保存像素信息.在playground中,在Sources
文件夹下建立一个新文件命名为pixel.swift.下一步,编写Pixel结构体.它只是一个简单结构体,各用一个变量来保存 RGBA 通道.咱们初始化alpha
通道为255,这意味着在[0~255]
范围的彻底不透明:数组
public struct Pixel {
var r: UInt8
var g: UInt8
var b: UInt8
var a: UInt8
init(red: UInt8, green: UInt8, blue: UInt8) {
r = red
g = green
b = blue
a = 255
}
}
复制代码
下一步,咱们须要建立一个数组来保存屏幕上的像素.为了计算每一个像素的颜色,咱们只须要给全部像素的Red
设置为0,同时Green
则从屏幕左下角的0(没有任何绿色)渐变到屏幕右上角的255(纯绿色).一样,Blue
颜色从屏幕顶部的0渐变到屏幕底部的255.数据结构
public func makePixelSet(width: Int, _ height: Int) -> ([Pixel], Int, Int) {
var pixel = Pixel(red: 0, green: 0, blue: 0)
var pixels = [Pixel](count: width * height, repeatedValue: pixel)
for i in 0..<width {
for j in 0..<height {
pixel = Pixel(red: 0, green: UInt8(Double(i * 255 / width)), blue: UInt8(Double(j * 255 / height)))
pixels[i + j * width] = pixel
}
}
return (pixels, width, height)
}
复制代码
最后,咱们须要一个方法来从像素建立出一个可绘制的图片.Core Image框架提供了CGImageCreate() 方法,它只须要几个参数就能够渲染图片:框架
public func imageFromPixels(pixels: ([Pixel], width: Int, height: Int)) -> CIImage {
let bitsPerComponent = 8
let bitsPerPixel = 32
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) // alpha is last
let providerRef = CGDataProviderCreateWithCFData(NSData(bytes: pixels.0, length: pixels.0.count * sizeof(Pixel)))
let image = CGImageCreate(pixels.1, pixels.2, bitsPerComponent, bitsPerPixel, pixels.1 * sizeof(Pixel), rgbColorSpace, bitmapInfo, providerRef, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
return CIImage(CGImage: image!)
}
复制代码
下一步,在playground主页中用给定的width
和height
建立一个窗口像素的集合,并用这个集合来建立渲染出的图片:ide
let width = 800
let height = 400
var pixelSet = makePixelSet(width, height)
var image = imageFromPixels(pixelSet)
image
复制代码
你应该能看到下面的图面:工具
OK,你可能会好奇,可是ray tracing射线追踪
在哪里呢?咱们稍后再解释这个.在Sources
文件夹下面,让咱们建立一个便利的类命名为vec3.swift,在里面放一些数学
工具方法:post
struct vec3 {
var x = 0.0
var y = 0.0
var z = 0.0
}
func * (left: Double, right: vec3) -> vec3 {
return vec3(x: left * right.x, y: left * right.y, z: left * right.z)
}
func + (left: vec3, right: vec3) -> vec3 {
return vec3(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)
}
func - (left: vec3, right: vec3) -> vec3 {
return vec3(x: left.x - right.x, y: left.y - right.y, z: left.z - right.z)
}
func dot (left: vec3, _ right: vec3) -> Double {
return left.x * right.x + left.y * right.y + left.z * right.z
}
func unit_vector(v: vec3) -> vec3 {
let length : Double = sqrt(dot(v, v))
return vec3(x: v.x/length, y: v.y/length, z: v.z/length)
}
复制代码
下一步,咱们须要建立一个ray射线结构体.它拥有一个origin原点
成员,一个direction方向
,还有一个方法能够计算任意给定参数的ray tracing
方程式:学习
struct ray {
var origin: vec3
var direction: vec3
func point_at_parameter(t: Double) -> vec3 {
return origin + t * direction
}
}
复制代码
而后咱们须要计算颜色,基于ray射线
是否接触到咱们在屏幕中间建立的sphere球体
:
func color(r: ray) -> vec3 {
let minusZ = vec3(x: 0, y: 0, z: -1.0)
var t = hit_sphere(minusZ, 0.5, r)
if t > 0.0 {
let norm = unit_vector(r.point_at_parameter(t) - minusZ)
return 0.5 * vec3(x: norm.x + 1.0, y: norm.y + 1.0, z: norm.z + 1.0)
}
let unit_direction = unit_vector(r.direction)
t = 0.5 * (unit_direction.y + 1.0)
return (1.0 - t) * vec3(x: 1.0, y: 1.0, z: 1.0) + t * vec3(x: 0.5, y: 0.7, z: 1.0)
}
复制代码
你注意到咱们用到了另外一个方法,叫hit_sphere(),来肯定咱们的射线是撞到了球体,或者没有接触球体:
func hit_sphere(center: vec3, _ radius: Double, _ r: ray) -> Double {
let oc = r.origin - center
let a = dot(r.direction, r.direction)
let b = 2.0 * dot(oc, r.direction)
let c = dot(oc, oc) - radius * radius
let discriminant = b * b - 4 * a * c
if discriminant < 0 {
return -1.0
} else {
return (-b - sqrt(discriminant)) / (2.0 * a)
}
}
复制代码
回到pixel.swift文件,更改makePixelSet(:),使其在每一个像素被加入集合前,给每一个像素建立一个ray射线
并计算它的color颜色
:
public func makePixelSet(width: Int, _ height: Int) -> ([Pixel], Int, Int) {
var pixel = Pixel(red: 0, green: 0, blue: 0)
var pixels = [Pixel](count: width * height, repeatedValue: pixel)
let lower_left_corner = vec3(x: -2.0, y: 1.0, z: -1.0)
let horizontal = vec3(x: 4.0, y: 0, z: 0)
let vertical = vec3(x: 0, y: -2.0, z: 0)
let origin = vec3()
for i in 0..<width {
for j in 0..<height {
let u = Double(i) / Double(width)
let v = Double(j) / Double(height)
let r = ray(origin: origin, direction: lower_left_corner + u * horizontal + v * vertical)
let col = color(r)
pixel = Pixel(red: UInt8(col.x * 255), green: UInt8(col.y * 255), blue: UInt8(col.z * 255))
pixels[i + j * width] = pixel
}
}
return (pixels, width, height)
}
复制代码
在playground的主页面,看看产生的新图像:
敬请关注本文的第2部分,咱们将会计算灯光和阴影,产生更真实的图像渲染.
源代码source code 已发布在Github上.
下次见!