-
Notifications
You must be signed in to change notification settings - Fork 6
/
GLLItemMesh+OBJExport.swift
55 lines (43 loc) · 1.99 KB
/
GLLItemMesh+OBJExport.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// GLLItemMesh+OBJExport.m
// GLLara
//
// Created by Torsten Kammer on 19.09.12.
// Copyright (c) 2012 Torsten Kammer. All rights reserved.
//
import Foundation
extension GLLItemMesh {
var willLoseDataWhenConvertedToOBJ: Bool {
return (self.mesh.textures.count > 1) || (self.mesh.textures.count == 1 && self.mesh.textures["diffuseTexture"]?.url == nil) || (self.renderParameters.count > 0)
}
func writeMTL(baseURL: URL) -> String {
var result = "newmtl material\(meshIndex)\r\n"
// Use only first texture and only if it isn't baked into the model file
// TODO It's probably possible to extract this texture, but does anyone care?
if let url = mesh.textures["diffuseTexture"]?.url {
let baseComponents = baseURL.pathComponents
let textureComponents = url.pathComponents
// Find where the paths diverge
var commonPathLength = 0
for i in 0 ..< min(baseComponents.count, textureComponents.count) {
if baseComponents[i] != textureComponents[i] {
break
} else {
commonPathLength = i
}
}
var relativePathComponents: [String] = []
// Add .. for any additional path in the base file
for _ in commonPathLength ..< baseComponents.count - 1 {
relativePathComponents.append("..")
}
relativePathComponents.append(contentsOf: textureComponents.dropFirst(commonPathLength))
let relativePath = relativePathComponents.joined(separator: "/")
result += "map_Kd \(relativePath)\r\n"
}
return result
}
func writeOBJ(transformations: [mat_float16], baseIndex: Int, includeColors: Bool) -> String {
return mesh.writeOBJ(transformations: transformations, baseIndex: baseIndex, includeColors: includeColors)
}
}