Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFUnarchiver + GLTFSceneSource: add embedExternalImages option #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kohlmannj
Copy link

During development on an internal tool, a colleague and I needed an option to suppress GLTFSceneKit’s default behavior of embedding external image data into a SceneKit file.

This PR achieves that by adding a embedExternalImages option to GTFSceneSource constructor, which is true by default, thereby matching GLTFSceneKit’s current behavior.

When the setting is false, a GLTFUnarchiver class instance will return a URL to an image, as extracted from a .gltf file, rather than Image data.

Copy link
Author

@kohlmannj kohlmannj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve self-reviewed with a few comments here. Please let me know if you have any additional feedback.

@@ -35,14 +35,16 @@ public class GLTFUnarchiver {
private var buffers: [Data?] = []
private var materials: [SCNMaterial?] = []
private var textures: [SCNMaterialProperty?] = []
private var images: [Image?] = []
// An array of Image or URL types
private var images: [Any?] = []
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that I’m new to Swift, and my most recent experience with typed languages is TypeScript. Unfortunately I couldn’t declare this as an array of Image | URL types. If Any is unacceptable, I would consider this instead:

Suggested change
private var images: [Any?] = []
private var images: [Image?] = []
private var imageUrls: [URL?] = []

@@ -771,7 +775,8 @@ public class GLTFUnarchiver {
return valueArray
}

private func loadImage(index: Int) throws -> Image {
// Returns either an Image or a URL
private func loadImage(index: Int) throws -> Any {
Copy link
Author

@kohlmannj kohlmannj Apr 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above comment regarding Any, I am willing to revise this part of the code to avoid Any as the return type. I would consider factoring out the shared logic here in loadImage() and then create a second private func loadImageUrl(index: Int) throws -> URL method.

image = try loadImageFile(from: url)
// Even if we shouldn't embed external images, try loading the image file for now
// TODO: figure out how to check if `url` exists on-disk rather than using `loadImageFile()` to do this
let loadedImage = try loadImageFile(from: url)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most awkward part of this revision. As indicated in the comments, I’m essentially using loadImageFile() as a test to see if the file exists on-disk. There are a few ways to improve this:

  • Add a helper function to simply check if the image URL exists on-disk when self.embedExternalImages is false
  • Call loadImageFile() only when self.embedExternalImages is true

@@ -785,7 +790,8 @@ public class GLTFUnarchiver {
}
let glImage = images[index]

var image: Image?
// An Image or a URL
var image: Any?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment regarding Any usage as the above comments (first comment, second comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants