The Skung Cave commit
This commit is contained in:
@ -8,25 +8,46 @@ public struct ObjReader
|
||||
var file = try ObjDocumentReader(filePath: url)
|
||||
|
||||
var model = ObjModel()
|
||||
var materials = Dictionary<String, ObjMaterial>()
|
||||
var name: String? = nil
|
||||
var object = ObjModel.Object()
|
||||
var mesh = ObjModel.Mesh()
|
||||
|
||||
file.string(tag: "mtllib") { s in
|
||||
let mats = try ObjMtlLoader.read(url: url.deletingLastPathComponent().appendingPathComponent(s[0]))
|
||||
materials.merge(mats, uniquingKeysWith: { (_, new) in new } )
|
||||
model.materials.merge(mats, uniquingKeysWith: { (_, new) in new } )
|
||||
}
|
||||
file.string(tag: "o", count: 1) { s in
|
||||
if !object.faces.isEmpty
|
||||
if !mesh.faces.isEmpty { object.meshes.append(mesh) }
|
||||
if !object.meshes.isEmpty
|
||||
{
|
||||
model.objects[name!] = object
|
||||
object = .init()
|
||||
mesh = .init()
|
||||
}
|
||||
name = String(s[0])
|
||||
}
|
||||
file.float(tag: "v", count: 3) { v in model.positions.append(Vec3f(v[0], v[1], v[2])) }
|
||||
file.float(tag: "v") { v in
|
||||
if v.count == 6
|
||||
{
|
||||
if model.colours.isEmpty && !model.positions.isEmpty
|
||||
{
|
||||
for _ in 0..<model.positions.count
|
||||
{
|
||||
model.colours.append(.one)
|
||||
}
|
||||
}
|
||||
model.colours.append(Vec3f(v[3], v[4], v[5]))
|
||||
}
|
||||
else if !model.colours.isEmpty && v.count == 3
|
||||
{
|
||||
model.colours.append(.one)
|
||||
}
|
||||
else if v.count != 3 { throw ObjLoaderError.badTagParameters }
|
||||
model.positions.append(Vec3f(v[0], v[1], v[2]))
|
||||
}
|
||||
file.float(tag: "vn", count: 3) { v in model.normals.append(Vec3f(v[0], v[1], v[2])) }
|
||||
file.float(tag: "vt", count: 2) { v in model.texCoords.append(Vec2f(v[0], v[1])) }
|
||||
file.int(tag: "l") { i in mesh.faces.append(.line(p1: i[0], p2: i[1])) }
|
||||
file.raw(tag: "f") { raw in
|
||||
let readIndex =
|
||||
{ (raw: Substring) in
|
||||
@ -40,14 +61,14 @@ public struct ObjReader
|
||||
if raw.count == 3
|
||||
{
|
||||
for raw in raw { _ = try readIndex(raw) }
|
||||
object.faces.append(.triangle(
|
||||
mesh.faces.append(.triangle(
|
||||
v1: try readIndex(raw[raw.startIndex]),
|
||||
v2: try readIndex(raw[raw.startIndex + 1]),
|
||||
v3: try readIndex(raw[raw.startIndex + 2])))
|
||||
}
|
||||
else if raw.count == 4
|
||||
{
|
||||
object.faces.append(.quad(
|
||||
mesh.faces.append(.quad(
|
||||
v1: try readIndex(raw[raw.startIndex]),
|
||||
v2: try readIndex(raw[raw.startIndex + 1]),
|
||||
v3: try readIndex(raw[raw.startIndex + 2]),
|
||||
@ -55,17 +76,25 @@ public struct ObjReader
|
||||
}
|
||||
else if raw.count >= 5
|
||||
{
|
||||
object.faces.append(.ngon(try raw.map { try readIndex($0) }))
|
||||
mesh.faces.append(.ngon(try raw.map { try readIndex($0) }))
|
||||
}
|
||||
else { throw ObjLoaderError.badTagParameters }
|
||||
}
|
||||
file.int(tag: "l") { i in object.faces.append(.line(p1: i[0], p2: i[1])) }
|
||||
file.string(tag: "usemtl", count: 1) { s in
|
||||
if !mesh.faces.isEmpty
|
||||
{
|
||||
object.meshes.append(mesh)
|
||||
mesh = .init()
|
||||
}
|
||||
mesh.material = s[0]
|
||||
}
|
||||
// file.int(tag: "s", count: 1) { i in } //TODO: Smoothing groups
|
||||
|
||||
try file.read()
|
||||
|
||||
if !object.faces.isEmpty
|
||||
{
|
||||
model.objects[name!] = object
|
||||
}
|
||||
if !mesh.faces.isEmpty { object.meshes.append(mesh) }
|
||||
if !object.meshes.isEmpty { model.objects[name!] = object }
|
||||
|
||||
return model
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user