Compare commits

...

3 Commits

Author SHA1 Message Date
Sergey Novikov c7ab788ae1 remove materail index 2019-10-14 13:18:49 +01:00
Sergey Novikov 94d33b1cfe remove buffer index 2019-10-14 11:29:29 +01:00
Sergey Novikov 1ee752c5c3 remove bufferviewindex 2019-10-14 11:17:44 +01:00
10 changed files with 88 additions and 74 deletions

View File

@ -34,8 +34,8 @@ extension GLTF {
if loadLevel == .all { if loadLevel == .all {
var buffers = [GLTFBuffer]() var buffers = [GLTFBuffer]()
for bViewIndex in descriptor.sources { for bView in descriptor.sources {
let buffer = self.buffers![self.bufferViews![bViewIndex].buffer] let buffer = bView.buffer
buffers.append(buffer) buffers.append(buffer)
} }
@ -65,28 +65,27 @@ extension GLTF {
} else { } else {
let sizeWidth = (loadLevel == .first) ? 32 : descriptor.width let sizeWidth = (loadLevel == .first) ? 32 : descriptor.width
let sizeHeight = (loadLevel == .first) ? 32 : descriptor.height let sizeHeight = (loadLevel == .first) ? 32 : descriptor.height
let index = (loadLevel == .first) ? descriptor.sources.last! : descriptor.sources.first! let bufferView = (loadLevel == .first) ? descriptor.sources.last! : descriptor.sources.first!
if let bView = self.bufferViews?[index] { let buffer_ = bufferView.buffer
let buffer_ = self.buffers![bView.buffer] self.loader.load(gltf:self, resource: buffer_, options: ResourceType.texture) { (buffer, error) in
self.loader.load(gltf:self, resource: buffer_, options: ResourceType.texture) { (buffer, error) in var error_ = error
var error_ = error var textureResult:Any?
var textureResult:Any? var datas = [Data]()
var datas = [Data]() if buffer.data != nil {
if buffer.data != nil { datas.append(buffer.data!)
datas.append(buffer.data!) do {
do { textureResult = try self._createMetalTexture(sizeWidth, sizeHeight, pixelFormat, datas, bytesPerRow)
textureResult = try self._createMetalTexture(sizeWidth, sizeHeight, pixelFormat, datas, bytesPerRow) } catch {
} catch { error_ = error
error_ = error
}
} else {
error_ = GLTFError("Can't load data for \(buffer.uri ?? "")")
} }
} else {
completionHandler(textureResult, error_) error_ = GLTFError("Can't load data for \(buffer.uri ?? "")")
} }
completionHandler(textureResult, error_)
} }
} }
} }

View File

@ -14,9 +14,9 @@ extension GLTF {
// MARK: - Material // MARK: - Material
// load material by index // load material by index
internal func loadMaterial(index:Int, delegate: TextureLoaderDelegate, textureChangedCallback: ((Any?)-> Void)? = nil, completionHandler: @escaping (SCNMaterial) -> Void) { internal func loadMaterial(_ material:GLTFMaterial?, delegate: TextureLoaderDelegate, textureChangedCallback: ((Any?)-> Void)? = nil, completionHandler: @escaping (SCNMaterial) -> Void) {
if let material = self.materials?[index] { if let material = material {
let scnMaterial = SCNMaterial() let scnMaterial = SCNMaterial()
scnMaterial.name = material.name scnMaterial.name = material.name
scnMaterial.isDoubleSided = material.doubleSided scnMaterial.isDoubleSided = material.doubleSided

View File

@ -104,27 +104,47 @@ extension GLTF {
} }
} }
internal static func requestData(glTF: GLTF, bufferView:GLTFBufferView) throws -> Data? { // internal static func requestData(glTF: GLTF, bufferView:GLTFBufferView) throws -> Data? {
if let buffer = glTF.buffers?[bufferView.buffer] { // if let buffer = glTF.buffers?[bufferView.buffer] {
//
// if let data = try glTF.loader.load(gltf:glTF, resource: buffer) {
// return data
// }
// } else {
// throw GLTFError("Can't load data! Can't find buffer at index \(bufferView.buffer)")
// }
// return nil
// }
if let data = try glTF.loader.load(gltf:glTF, resource: buffer) { // internal static func requestData(glTF: GLTF, bufferView:Int) throws -> (GLTFBufferView, Data)? {
return data // if let bufferView = glTF.bufferViews?[bufferView] {
} // if let data = try requestData(glTF: glTF, bufferView: bufferView) {
} else { // return (bufferView, data)
// }
// } else {
// throw GLTFError("Can't load data! Can't find bufferView at index \(bufferView)")
// }
// return nil
// }
internal static func requestData(glTF: GLTF, bufferView:GLTFBufferView) throws -> (GLTFBufferView, Data)? {
// if let bufferView = glTF.bufferViews?[bufferView] {
// if let data = try requestData(glTF: glTF, bufferView: bufferView) {
// return (bufferView, data)
// }
// } else {
// throw GLTFError("Can't load data! Can't find bufferView at index \(bufferView)")
// }
// return nil
let buffer = bufferView.buffer
if let data = try glTF.loader.load(gltf:glTF, resource: buffer) {
return (bufferView, data)
}
else {
throw GLTFError("Can't load data! Can't find buffer at index \(bufferView.buffer)") throw GLTFError("Can't load data! Can't find buffer at index \(bufferView.buffer)")
} }
return nil
}
internal static func requestData(glTF: GLTF, bufferView:Int) throws -> (GLTFBufferView, Data)? {
if let bufferView = glTF.bufferViews?[bufferView] {
if let data = try requestData(glTF: glTF, bufferView: bufferView) {
return (bufferView, data)
}
} else {
throw GLTFError("Can't load data! Can't find bufferView at index \(bufferView)")
}
return nil
} }
} }

View File

@ -111,7 +111,7 @@ import Foundation
@objcMembers @objcMembers
open class GLTFAccessor : NSObject, Codable { open class GLTFAccessor : NSObject, Codable {
/// The index of the bufferView. /// The index of the bufferView.
public var bufferView:Int? public var bufferView:GLTFBufferView?
/// The offset relative to the start of the bufferView in bytes. /// The offset relative to the start of the bufferView in bytes.
public var byteOffset:Int public var byteOffset:Int
@ -163,7 +163,7 @@ open class GLTFAccessor : NSObject, Codable {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
bufferView = try? container.decode(Int.self, forKey: .bufferView) bufferView = try? container.decode(GLTFBufferView.self, forKey: .bufferView)
do { do {
byteOffset = try container.decode(Int.self, forKey: .byteOffset) byteOffset = try container.decode(Int.self, forKey: .byteOffset)
} catch { } catch {

View File

@ -40,7 +40,7 @@ import Foundation
@objcMembers @objcMembers
open class GLTFBufferView : NSObject, Codable { open class GLTFBufferView : NSObject, Codable {
/// The index of the buffer. /// The index of the buffer.
public var buffer:Int public var buffer:GLTFBuffer
/// The length of the bufferView in bytes. /// The length of the bufferView in bytes.
public var byteLength:Int public var byteLength:Int
@ -74,7 +74,7 @@ open class GLTFBufferView : NSObject, Codable {
case target case target
} }
public init(buffer b:Int, byteLength bl:Int, byteOffset bo:Int) { public init(buffer b:GLTFBuffer, byteLength bl:Int, byteOffset bo:Int) {
buffer = b buffer = b
byteLength = bl byteLength = bl
byteOffset = bo byteOffset = bo
@ -82,7 +82,7 @@ open class GLTFBufferView : NSObject, Codable {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
buffer = try container.decode(Int.self, forKey: .buffer) buffer = try container.decode(GLTFBuffer.self, forKey: .buffer)
byteLength = try container.decode(Int.self, forKey: .byteLength) byteLength = try container.decode(Int.self, forKey: .byteLength)
do { do {
byteOffset = try container.decode(Int.self, forKey: .byteOffset) byteOffset = try container.decode(Int.self, forKey: .byteOffset)

View File

@ -40,7 +40,7 @@ import Foundation
@objcMembers @objcMembers
open class GLTFImage : NSObject, Codable { open class GLTFImage : NSObject, Codable {
/// The index of the bufferView that contains the image. Use this instead of the image's uri property. /// The index of the bufferView that contains the image. Use this instead of the image's uri property.
public var bufferView:Int? public var bufferView:GLTFBufferView?
/// Dictionary object with extension-specific objects. /// Dictionary object with extension-specific objects.
public var extensions:[String: Any]? public var extensions:[String: Any]?
@ -70,7 +70,7 @@ open class GLTFImage : NSObject, Codable {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
bufferView = try? container.decode(Int.self, forKey: .bufferView) bufferView = try? container.decode(GLTFBufferView.self, forKey: .bufferView)
extensions = try? container.decode([String: Any].self, forKey: .extensions) extensions = try? container.decode([String: Any].self, forKey: .extensions)
extras = try? container.decode([String: Any].self, forKey: .extras) extras = try? container.decode([String: Any].self, forKey: .extras)
mimeType = try? container.decode(GLTFImageMimeType.self, forKey: .mimeType) mimeType = try? container.decode(GLTFImageMimeType.self, forKey: .mimeType)

View File

@ -17,14 +17,14 @@ open class GLTFKHRDracoMeshCompressionExtension : NSObject, Codable {
public var attributes:[String: Int] public var attributes:[String: Int]
/// The index of the bufferView. /// The index of the bufferView.
public var bufferView:Int public var bufferView:GLTFBufferView
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case attributes case attributes
case bufferView case bufferView
} }
public init(attributes a:[String: Int], bufferView bv:Int) { public init(attributes a:[String: Int], bufferView bv:GLTFBufferView) {
attributes = a attributes = a
bufferView = bv bufferView = bv
} }
@ -32,7 +32,7 @@ open class GLTFKHRDracoMeshCompressionExtension : NSObject, Codable {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
attributes = try container.decode([String: Int].self, forKey: .attributes) attributes = try container.decode([String: Int].self, forKey: .attributes)
bufferView = try container.decode(Int.self, forKey: .bufferView) bufferView = try container.decode(GLTFBufferView.self, forKey: .bufferView)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {

View File

@ -80,7 +80,7 @@ open class GLTFMeshPrimitive : NSObject, Codable {
public var indices:Int? public var indices:Int?
/// The index of the material to apply to this primitive when rendering. /// The index of the material to apply to this primitive when rendering.
public var material:Int? public var material:GLTFMaterial?
/// The type of primitives to render. /// The type of primitives to render.
public var mode:GLTFMeshPrimitiveMode public var mode:GLTFMeshPrimitiveMode
@ -109,7 +109,7 @@ open class GLTFMeshPrimitive : NSObject, Codable {
extensions = try? container.decode([String: GLTFKHRDracoMeshCompressionExtension].self, forKey: .extensions) extensions = try? container.decode([String: GLTFKHRDracoMeshCompressionExtension].self, forKey: .extensions)
extras = try? container.decode([String: Any].self, forKey: .extras) extras = try? container.decode([String: Any].self, forKey: .extras)
indices = try? container.decode(Int.self, forKey: .indices) indices = try? container.decode(Int.self, forKey: .indices)
material = try? container.decode(Int.self, forKey: .material) material = try? container.decode(GLTFMaterial.self, forKey: .material)
do { do {
mode = try container.decode(GLTFMeshPrimitiveMode.self, forKey: .mode) mode = try container.decode(GLTFMeshPrimitiveMode.self, forKey: .mode)
} catch { } catch {

View File

@ -123,12 +123,8 @@ extension GLTFAccessor {
fileprivate extension GLTF { fileprivate extension GLTF {
//let buffer = glTF.buffers![glTF.bufferViews![bufferView].buffer] //let buffer = glTF.buffers![glTF.bufferViews![bufferView].buffer]
func buffer(for bufferView: Int) -> GLTFBuffer? { func buffer(for bufferView: GLTFBufferView) -> GLTFBuffer? {
if let bufferIndex = self.bufferViews?[bufferView].buffer { return bufferView.buffer
return self.buffers?[bufferIndex]
}
return nil
} }
} }
@ -213,8 +209,8 @@ extension GLTFConverter {
primitiveNode.geometry!.firstMaterial = emptyMaterial primitiveNode.geometry!.firstMaterial = emptyMaterial
} }
if let materialIndex = primitive.material { if let material = primitive.material {
self.glTF.loadMaterial(index:materialIndex, delegate: self, textureChangedCallback: { _ in self.glTF.loadMaterial(material, delegate: self, textureChangedCallback: { _ in
if let material = primitiveNode.geometry?.firstMaterial { if let material = primitiveNode.geometry?.firstMaterial {
if let texture = material.diffuse.contents as? MTLTexture { if let texture = material.diffuse.contents as? MTLTexture {
if texture.pixelFormat.hasAlpha() { if texture.pixelFormat.hasAlpha() {
@ -268,8 +264,7 @@ extension GLTFConverter {
fileprivate func geometryElement(_ primitive: GLTFMeshPrimitive) throws -> SCNGeometryElement? { fileprivate func geometryElement(_ primitive: GLTFMeshPrimitive) throws -> SCNGeometryElement? {
if let indicesIndex = primitive.indices { if let indicesIndex = primitive.indices {
if let accessor = glTF.accessors?[indicesIndex], if let accessor = glTF.accessors?[indicesIndex],
let bufferViewIndex = accessor.bufferView, let bufferView = accessor.bufferView {
let bufferView = glTF.bufferViews?[bufferViewIndex] {
if let indicesData = try loadAcessor(accessor, bufferView, false) { if let indicesData = try loadAcessor(accessor, bufferView, false) {
@ -309,7 +304,7 @@ extension GLTFConverter {
var geometrySources = [SCNGeometrySource]() var geometrySources = [SCNGeometrySource]()
// accessors can point to different buffers. We cache last one. // accessors can point to different buffers. We cache last one.
var previousBufferView = -1 var previousBufferView: GLTFBufferView?
var mtlBuffer:MTLBuffer? var mtlBuffer:MTLBuffer?
var byteOffset = 0 var byteOffset = 0
@ -417,11 +412,11 @@ extension GLTFConverter {
// determine where an accessor and a bufferView link are interleaved or not // determine where an accessor and a bufferView link are interleaved or not
internal func determineAcessor(_ accessor:GLTFAccessor) throws -> (GLTFBufferView, Bool)? { internal func determineAcessor(_ accessor:GLTFAccessor) throws -> (GLTFBufferView, Bool)? {
guard let index = accessor.bufferView else { guard let bufferView = accessor.bufferView else {
throw GLTFError("Missing 'bufferView' for \(accessor.name ?? "") acessor") throw GLTFError("Missing 'bufferView' for \(accessor.name ?? "") acessor")
} }
if let bufferView = glTF.bufferViews?[index] { // if let bufferView = glTF.bufferViews?[index] {
// Interleaved data usualy has bytesStride as correct value. // Interleaved data usualy has bytesStride as correct value.
// Some times non-interleaved data also has bytesStride, and in some cases don't. It's up to exporter // Some times non-interleaved data also has bytesStride, and in some cases don't. It's up to exporter
@ -431,15 +426,15 @@ extension GLTFConverter {
let interleaved = (byteStride != accessorByteStride) let interleaved = (byteStride != accessorByteStride)
return (bufferView, interleaved) return (bufferView, interleaved)
} // }
return nil // return nil
} }
// get data by accessor // get data by accessor
internal func loadAcessor(_ accessor:GLTFAccessor, _ bufferView:GLTFBufferView, _ interleaved:Bool) throws -> Data? { internal func loadAcessor(_ accessor:GLTFAccessor, _ bufferView:GLTFBufferView, _ interleaved:Bool) throws -> Data? {
if let data = try GLTF.requestData(glTF: glTF, bufferView: bufferView) { if let (bufferView, data) = try GLTF.requestData(glTF: glTF, bufferView: bufferView) {
var byteStride:Int = bufferView.byteStride ?? 0 var byteStride:Int = bufferView.byteStride ?? 0
if (byteStride == 0) { if (byteStride == 0) {

View File

@ -357,7 +357,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
public var height:Int public var height:Int
/// Texture index of bufferView used for each level of texture. First source representing level 0. Each next is divide by 2 of previous texture size. For example 0 level is 1024, next is 512 and next 256 ans so on. /// Texture index of bufferView used for each level of texture. First source representing level 0. Each next is divide by 2 of previous texture size. For example 0 level is 1024, next is 512 and next 256 ans so on.
public var sources:[Int] public var sources:[GLTFBufferView]
/// Texture 2D target. /// Texture 2D target.
public var target:GLTF_3D4MCompressedTextureExtensionTarget public var target:GLTF_3D4MCompressedTextureExtensionTarget
@ -373,7 +373,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
public init(compression c:GLTF_3D4MCompressedTextureExtensionCompression, public init(compression c:GLTF_3D4MCompressedTextureExtensionCompression,
width w:Int, width w:Int,
height h:Int, height h:Int,
sources s:[Int], sources s:[GLTFBufferView],
target t:GLTF_3D4MCompressedTextureExtensionTarget) { target t:GLTF_3D4MCompressedTextureExtensionTarget) {
compression = c compression = c
width = w width = w
@ -390,7 +390,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
} catch { } catch {
height = 0 height = 0
} }
sources = try container.decode([Int].self, forKey: .sources) sources = try container.decode([GLTFBufferView].self, forKey: .sources)
do { do {
target = try container.decode(GLTF_3D4MCompressedTextureExtensionTarget.self, forKey: .target) target = try container.decode(GLTF_3D4MCompressedTextureExtensionTarget.self, forKey: .target)
} catch { } catch {