Skip to content

Commit

Permalink
Attach custom attributes to SCNNode
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresjames committed Jul 19, 2024
1 parent 6e9ca16 commit f5f9020
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions GLTFKit2/GLTFKit2/GLTFSceneKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

NS_ASSUME_NONNULL_BEGIN

extern const void *GLTFKit2MetadataKey;
@interface GLTFKit2Metadata : NSObject
+ (void)setMetadata:(NSDictionary *)metadata forNode:(SCNNode *)node;
+ (NSDictionary *_Nullable)getMetadata:(SCNNode *)node;
@end

extern NSString *const GLTFAssetPropertyKeyCopyright;
extern NSString *const GLTFAssetPropertyKeyGenerator;
extern NSString *const GLTFAssetPropertyKeyVersion;
Expand Down
30 changes: 26 additions & 4 deletions GLTFKit2/GLTFKit2/GLTFSceneKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "GLTFLogging.h"
#import "GLTFWorkflowHelper.h"

#import <objc/runtime.h>
#import <SceneKit/ModelIO.h>
#import <simd/simd.h>

Expand All @@ -13,6 +14,16 @@
} \
} while(0)

const void *GLTFKit2MetadataKey = &GLTFKit2MetadataKey;
@implementation GLTFKit2Metadata
+ (void)setMetadata:(NSDictionary *)metadata forNode:(SCNNode *)node {
objc_setAssociatedObject(node, GLTFKit2MetadataKey, metadata, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (NSDictionary *_Nullable)getMetadata:(SCNNode *)node {
return objc_getAssociatedObject(node, GLTFKit2MetadataKey);
}
@end

NSString *const GLTFAssetPropertyKeyCopyright = @"GLTFAssetPropertyKeyCopyright";
NSString *const GLTFAssetPropertyKeyGenerator = @"GLTFAssetPropertyKeyGenerator";
NSString *const GLTFAssetPropertyKeyVersion = @"GLTFAssetPropertyKeyVersion";
Expand Down Expand Up @@ -503,7 +514,7 @@ static BOOL GLTFSCNNativelySupportsAccessor(GLTFAccessor *accessor, NSString *se
}
}
}

// Prior to macOS 14.0 and iOS 17.0, hit-testing against nodes whose bone indices were in ushort format
// did not work for GPU-skinned meshes. On older platforms, we transform from ushort indices to uchar
// indices iff the transform is lossless (i.e., if all indices are < 256).
Expand Down Expand Up @@ -892,6 +903,7 @@ - (BOOL)convertAsset {

NSMutableDictionary <NSUUID *, SCNGeometry *> *geometryForIdentifiers = [NSMutableDictionary dictionary];
NSMutableDictionary <NSUUID *, SCNGeometryElement *> *geometryElementForIdentifiers = [NSMutableDictionary dictionary];
NSMutableDictionary <NSUUID *, NSMutableArray<NSString *> *> *metadata = [NSMutableDictionary dictionary];
for (GLTFMesh *mesh in self.asset.meshes) {
for (GLTFPrimitive *primitive in mesh.primitives) {
int vertexCount = 0;
Expand Down Expand Up @@ -949,9 +961,14 @@ - (BOOL)convertAsset {
// For primitive types not supported by SceneKit (line loops, line strips, triangle
// fans), we retopologize the primitive's indices. However, if they aren't present,
// we need to adjust the vertex data.
if ( [attribute.name isEqual:@"WEIGHTS_0"]
|| [attribute.name isEqual:@"JOINTS_0"]
|| [attribute.name hasPrefix:@"_"]) {
if ([attribute.name hasPrefix:@"_"]) {
if (metadata[attribute.name] == nil) {
metadata[attribute.name] = [NSMutableArray array];
}
[metadata[attribute.name] addObject:attribute.accessor.identifier.UUIDString];
continue;
} else if ( [attribute.name isEqual:@"WEIGHTS_0"]
|| [attribute.name isEqual:@"JOINTS_0"]) {
// Omit joint indices and weights; these are stored later on the skinner
continue;
}
Expand Down Expand Up @@ -1069,6 +1086,11 @@ - (BOOL)convertAsset {
scnNode.light = lights[lightIndex];
}

// Add metadata to scnNode
if (metadata.count > 0) {
[GLTFKit2Metadata setMetadata:metadata forNode:scnNode];
}

// This collection holds the nodes to which any skin on this node should be applied,
// since we don't have a one-to-one mapping from nodes to meshes. It's also used to
// apply morph targets to the correct primitives.
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let package = Package(
],
targets: [
.binaryTarget(name: "GLTFKit2",
url: "https://github.com/Sitelink-Spatial/GLTFKit2/releases/download/R4/GLTFKit2.xcframework.zip",
checksum: "87fa6596f8fbe16fa2d1b0564236e45f32da13b90c074aa23b3a5c3f1d5ca89d"
url: "https://github.com/Sitelink-Spatial/GLTFKit2/releases/download/R5/GLTFKit2.xcframework.zip",
checksum: "0014a29edfce9301449e47b8e89be66f0ab19ed04dfe7fcfc955ae02bb38cede"
)
]
)
4 changes: 3 additions & 1 deletion make-xcframework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set -e # Exit on error

moduleName="GLTFKit2"

buildType="Release"
buildType="${1:-Release}"

echo "Building $moduleName for $buildType"

outputDirectory="$(pwd;)/$moduleName.xcframework"

Expand Down

0 comments on commit f5f9020

Please sign in to comment.