Compare commits
3 Commits
develop
...
feature/im
Author | SHA1 | Date |
---|---|---|
![]() |
c7ab788ae1 | |
![]() |
94d33b1cfe | |
![]() |
1ee752c5c3 |
|
@ -34,8 +34,8 @@ extension GLTF {
|
|||
|
||||
if loadLevel == .all {
|
||||
var buffers = [GLTFBuffer]()
|
||||
for bViewIndex in descriptor.sources {
|
||||
let buffer = self.buffers![self.bufferViews![bViewIndex].buffer]
|
||||
for bView in descriptor.sources {
|
||||
let buffer = bView.buffer
|
||||
buffers.append(buffer)
|
||||
}
|
||||
|
||||
|
@ -65,28 +65,27 @@ extension GLTF {
|
|||
} else {
|
||||
let sizeWidth = (loadLevel == .first) ? 32 : descriptor.width
|
||||
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_ = self.buffers![bView.buffer]
|
||||
self.loader.load(gltf:self, resource: buffer_, options: ResourceType.texture) { (buffer, error) in
|
||||
var error_ = error
|
||||
var textureResult:Any?
|
||||
var datas = [Data]()
|
||||
if buffer.data != nil {
|
||||
datas.append(buffer.data!)
|
||||
do {
|
||||
textureResult = try self._createMetalTexture(sizeWidth, sizeHeight, pixelFormat, datas, bytesPerRow)
|
||||
} catch {
|
||||
error_ = error
|
||||
}
|
||||
} else {
|
||||
error_ = GLTFError("Can't load data for \(buffer.uri ?? "")")
|
||||
let buffer_ = bufferView.buffer
|
||||
self.loader.load(gltf:self, resource: buffer_, options: ResourceType.texture) { (buffer, error) in
|
||||
var error_ = error
|
||||
var textureResult:Any?
|
||||
var datas = [Data]()
|
||||
if buffer.data != nil {
|
||||
datas.append(buffer.data!)
|
||||
do {
|
||||
textureResult = try self._createMetalTexture(sizeWidth, sizeHeight, pixelFormat, datas, bytesPerRow)
|
||||
} catch {
|
||||
error_ = error
|
||||
}
|
||||
|
||||
completionHandler(textureResult, error_)
|
||||
} else {
|
||||
error_ = GLTFError("Can't load data for \(buffer.uri ?? "")")
|
||||
}
|
||||
|
||||
completionHandler(textureResult, error_)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,9 +14,9 @@ extension GLTF {
|
|||
// MARK: - Material
|
||||
|
||||
// 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()
|
||||
scnMaterial.name = material.name
|
||||
scnMaterial.isDoubleSided = material.doubleSided
|
||||
|
|
|
@ -104,27 +104,47 @@ extension GLTF {
|
|||
}
|
||||
}
|
||||
|
||||
internal static func requestData(glTF: GLTF, bufferView:GLTFBufferView) throws -> Data? {
|
||||
if let buffer = glTF.buffers?[bufferView.buffer] {
|
||||
// internal static func requestData(glTF: GLTF, bufferView:GLTFBufferView) throws -> Data? {
|
||||
// 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) {
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
// 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
|
||||
// }
|
||||
|
||||
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)")
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ import Foundation
|
|||
@objcMembers
|
||||
open class GLTFAccessor : NSObject, Codable {
|
||||
/// The index of the bufferView.
|
||||
public var bufferView:Int?
|
||||
public var bufferView:GLTFBufferView?
|
||||
|
||||
/// The offset relative to the start of the bufferView in bytes.
|
||||
public var byteOffset:Int
|
||||
|
@ -163,7 +163,7 @@ open class GLTFAccessor : NSObject, Codable {
|
|||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
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 {
|
||||
byteOffset = try container.decode(Int.self, forKey: .byteOffset)
|
||||
} catch {
|
||||
|
|
|
@ -40,7 +40,7 @@ import Foundation
|
|||
@objcMembers
|
||||
open class GLTFBufferView : NSObject, Codable {
|
||||
/// The index of the buffer.
|
||||
public var buffer:Int
|
||||
public var buffer:GLTFBuffer
|
||||
|
||||
/// The length of the bufferView in bytes.
|
||||
public var byteLength:Int
|
||||
|
@ -74,7 +74,7 @@ open class GLTFBufferView : NSObject, Codable {
|
|||
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
|
||||
byteLength = bl
|
||||
byteOffset = bo
|
||||
|
@ -82,7 +82,7 @@ open class GLTFBufferView : NSObject, Codable {
|
|||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
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)
|
||||
do {
|
||||
byteOffset = try container.decode(Int.self, forKey: .byteOffset)
|
||||
|
|
|
@ -40,7 +40,7 @@ import Foundation
|
|||
@objcMembers
|
||||
open class GLTFImage : NSObject, Codable {
|
||||
/// 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.
|
||||
public var extensions:[String: Any]?
|
||||
|
@ -70,7 +70,7 @@ open class GLTFImage : NSObject, Codable {
|
|||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
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)
|
||||
extras = try? container.decode([String: Any].self, forKey: .extras)
|
||||
mimeType = try? container.decode(GLTFImageMimeType.self, forKey: .mimeType)
|
||||
|
|
|
@ -17,14 +17,14 @@ open class GLTFKHRDracoMeshCompressionExtension : NSObject, Codable {
|
|||
public var attributes:[String: Int]
|
||||
|
||||
/// The index of the bufferView.
|
||||
public var bufferView:Int
|
||||
public var bufferView:GLTFBufferView
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case attributes
|
||||
case bufferView
|
||||
}
|
||||
|
||||
public init(attributes a:[String: Int], bufferView bv:Int) {
|
||||
public init(attributes a:[String: Int], bufferView bv:GLTFBufferView) {
|
||||
attributes = a
|
||||
bufferView = bv
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ open class GLTFKHRDracoMeshCompressionExtension : NSObject, Codable {
|
|||
required public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
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 {
|
||||
|
|
|
@ -80,7 +80,7 @@ open class GLTFMeshPrimitive : NSObject, Codable {
|
|||
public var indices:Int?
|
||||
|
||||
/// 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.
|
||||
public var mode:GLTFMeshPrimitiveMode
|
||||
|
@ -109,7 +109,7 @@ open class GLTFMeshPrimitive : NSObject, Codable {
|
|||
extensions = try? container.decode([String: GLTFKHRDracoMeshCompressionExtension].self, forKey: .extensions)
|
||||
extras = try? container.decode([String: Any].self, forKey: .extras)
|
||||
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 {
|
||||
mode = try container.decode(GLTFMeshPrimitiveMode.self, forKey: .mode)
|
||||
} catch {
|
||||
|
|
|
@ -123,12 +123,8 @@ extension GLTFAccessor {
|
|||
|
||||
fileprivate extension GLTF {
|
||||
//let buffer = glTF.buffers![glTF.bufferViews![bufferView].buffer]
|
||||
func buffer(for bufferView: Int) -> GLTFBuffer? {
|
||||
if let bufferIndex = self.bufferViews?[bufferView].buffer {
|
||||
return self.buffers?[bufferIndex]
|
||||
}
|
||||
|
||||
return nil
|
||||
func buffer(for bufferView: GLTFBufferView) -> GLTFBuffer? {
|
||||
return bufferView.buffer
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -213,8 +209,8 @@ extension GLTFConverter {
|
|||
primitiveNode.geometry!.firstMaterial = emptyMaterial
|
||||
}
|
||||
|
||||
if let materialIndex = primitive.material {
|
||||
self.glTF.loadMaterial(index:materialIndex, delegate: self, textureChangedCallback: { _ in
|
||||
if let material = primitive.material {
|
||||
self.glTF.loadMaterial(material, delegate: self, textureChangedCallback: { _ in
|
||||
if let material = primitiveNode.geometry?.firstMaterial {
|
||||
if let texture = material.diffuse.contents as? MTLTexture {
|
||||
if texture.pixelFormat.hasAlpha() {
|
||||
|
@ -268,8 +264,7 @@ extension GLTFConverter {
|
|||
fileprivate func geometryElement(_ primitive: GLTFMeshPrimitive) throws -> SCNGeometryElement? {
|
||||
if let indicesIndex = primitive.indices {
|
||||
if let accessor = glTF.accessors?[indicesIndex],
|
||||
let bufferViewIndex = accessor.bufferView,
|
||||
let bufferView = glTF.bufferViews?[bufferViewIndex] {
|
||||
let bufferView = accessor.bufferView {
|
||||
|
||||
if let indicesData = try loadAcessor(accessor, bufferView, false) {
|
||||
|
||||
|
@ -309,7 +304,7 @@ extension GLTFConverter {
|
|||
var geometrySources = [SCNGeometrySource]()
|
||||
|
||||
// accessors can point to different buffers. We cache last one.
|
||||
var previousBufferView = -1
|
||||
var previousBufferView: GLTFBufferView?
|
||||
var mtlBuffer:MTLBuffer?
|
||||
|
||||
var byteOffset = 0
|
||||
|
@ -417,11 +412,11 @@ extension GLTFConverter {
|
|||
// determine where an accessor and a bufferView link are interleaved or not
|
||||
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")
|
||||
}
|
||||
|
||||
if let bufferView = glTF.bufferViews?[index] {
|
||||
// if let bufferView = glTF.bufferViews?[index] {
|
||||
|
||||
// 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
|
||||
|
@ -431,15 +426,15 @@ extension GLTFConverter {
|
|||
|
||||
let interleaved = (byteStride != accessorByteStride)
|
||||
return (bufferView, interleaved)
|
||||
}
|
||||
return nil
|
||||
// }
|
||||
// return nil
|
||||
}
|
||||
|
||||
|
||||
// get data by accessor
|
||||
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
|
||||
if (byteStride == 0) {
|
||||
|
|
|
@ -357,7 +357,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
|
|||
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.
|
||||
public var sources:[Int]
|
||||
public var sources:[GLTFBufferView]
|
||||
|
||||
/// Texture 2D target.
|
||||
public var target:GLTF_3D4MCompressedTextureExtensionTarget
|
||||
|
@ -373,7 +373,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
|
|||
public init(compression c:GLTF_3D4MCompressedTextureExtensionCompression,
|
||||
width w:Int,
|
||||
height h:Int,
|
||||
sources s:[Int],
|
||||
sources s:[GLTFBufferView],
|
||||
target t:GLTF_3D4MCompressedTextureExtensionTarget) {
|
||||
compression = c
|
||||
width = w
|
||||
|
@ -390,7 +390,7 @@ open class GLTF_3D4MCompressedTextureExtension : Codable {
|
|||
} catch {
|
||||
height = 0
|
||||
}
|
||||
sources = try container.decode([Int].self, forKey: .sources)
|
||||
sources = try container.decode([GLTFBufferView].self, forKey: .sources)
|
||||
do {
|
||||
target = try container.decode(GLTF_3D4MCompressedTextureExtensionTarget.self, forKey: .target)
|
||||
} catch {
|
||||
|
|
Loading…
Reference in New Issue