-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathRCTARKitNodes.m
More file actions
407 lines (312 loc) · 13.4 KB
/
RCTARKitNodes.m
File metadata and controls
407 lines (312 loc) · 13.4 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// RCTARKitNodes.m
// RCTARKit
//
// Created by Zehao Li on 9/9/17.
// Copyright © 2017 HippoAR. All rights reserved.
//
#import "RCTARKit.h"
#import "RCTARKitNodes.h"
#import "RCTConvert+ARKit.h"
@implementation SCNNode (ReferenceFrame)
@dynamic referenceFrame;
@end
CGFloat focDistance = 0.45f;
@interface RCTARKitNodes () <RCTARKitSessionDelegate>
@property (nonatomic, strong) SCNNode* rootNode;
@property NSMutableDictionary *nodes;
@property NSMutableDictionary *orphans;
@end
@implementation RCTARKitNodes
+ (instancetype)sharedInstance {
static RCTARKitNodes *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (instance == nil) {
instance = [[self alloc] init];
}
});
return instance;
}
- (instancetype)init {
if ((self = [super init])) {
// local reference frame origin
self.localOrigin = [[SCNNode alloc] init];
self.localOrigin.name = @"localOrigin";
// camera reference frame origin
self.cameraOrigin = [[SCNNode alloc] init];
self.cameraOrigin.name = @"cameraOrigin";
// front-of-camera frame origin
self.frontOfCamera = [[SCNNode alloc] init];
self.frontOfCamera.name = @"frontOfCamera";
// init caches
self.nodes = [NSMutableDictionary new];
self.orphans = [NSMutableDictionary new];
}
return self;
}
- (void)setArView:(ARSCNView *)arView {
//NSLog(@"setArView");
_arView = arView;
self.rootNode = arView.scene.rootNode;
self.rootNode.name = @"root";
[self.rootNode addChildNode:self.localOrigin];
[self.rootNode addChildNode:self.cameraOrigin];
[self.rootNode addChildNode:self.frontOfCamera];
}
#pragma mark
/**
add a node to scene on a frame (defaults to Local) or to a parentNode (if parentId is given)
*/
- (void)addNodeToScene:(SCNNode *)node inReferenceFrame:(NSString *)referenceFrame withParentId:(NSString *)parentId {
//NSLog(@"addNodeToScene node: %@ ", node.name);
if(parentId) {
[self addNodeToParent:node parentId:parentId];
} else {
[self addNodeToFrame:node referenceFrame:referenceFrame];
}
}
/**
add a node to scene in a reference frame
*/
- (void)addNodeToFrame:(SCNNode *)node referenceFrame:(NSString *)referenceFrame {
[self registerNode:node withId:node.name];
if (!referenceFrame) {
referenceFrame = @"Local"; // default to Local frame
}
NSString *selectorString = [NSString stringWithFormat:@"addNodeTo%@Frame:", referenceFrame];
SEL selector = NSSelectorFromString(selectorString);
if ([self respondsToSelector:selector]) {
// check https://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown
IMP imp = [self methodForSelector:selector];
void (*func)(id, SEL, SCNNode*) = (void *)imp;
func(self, selector, node);
}
}
- (void)addNodeToParent:(SCNNode *)node parentId:(NSString *)parentId {
SCNNode * parentNode = [self getNodeWithId:parentId];
if(!parentNode) {
NSMutableSet * orphansLookingForParent = [self.orphans objectForKey:parentId];
if(!orphansLookingForParent) {
orphansLookingForParent = [NSMutableSet setWithObject:node];
} else {
[orphansLookingForParent addObject:node];
}
[self.orphans setObject:orphansLookingForParent forKey:parentId];
} else {
[parentNode addChildNode:node];
}
[self registerNode:node withId:node.name];
}
- (void)clear {
// clear scene
NSArray *keys = [self.nodes allKeys];
for (id key in keys) {
id node = [self.nodes objectForKey:key];
if (node) {
[node removeFromParentNode];
}
}
[self.nodes removeAllObjects];
}
- (void)addNodeToLocalFrame:(SCNNode *)node {
node.referenceFrame = RFReferenceFrameLocal;
[self.localOrigin addChildNode:node];
//NSLog(@"[RCTARKitNodes] Add node %@ to Local frame at (%.2f, %.2f, %.2f)", node.name, node.position.x, node.position.y, node.position.z);
}
- (void)addNodeToCameraFrame:(SCNNode *)node {
node.referenceFrame = RFReferenceFrameCamera;
//NSLog(@"[RCTARKitNodes] Add node %@ to Camera frame at (%.2f, %.2f, %.2f)", node.name, node.position.x, node.position.y, node.position.z);
[self.cameraOrigin addChildNode:node];
}
- (void)addNodeToFrontOfCameraFrame:(SCNNode *)node {
node.referenceFrame = RFReferenceFrameFrontOfCamera;
//NSLog(@"[RCTARKitNodes] Add node %@ to FrontOfCamera frame at (%.2f, %.2f, %.2f)", node.name, node.position.x, node.position.y, node.position.z);
[self.frontOfCamera addChildNode:node];
}
- (NSDictionary *)getSceneObjectsHitResult:(const CGPoint)tapPoint {
NSDictionary *options = @{
SCNHitTestRootNodeKey: self.localOrigin,
SCNHitTestSortResultsKey: @(YES),
SCNHitTestOptionSearchMode: @(SCNHitTestSearchModeAll)
};
NSArray<SCNHitTestResult *> *results = [_arView hitTest:tapPoint options:options];
NSMutableArray * resultsMapped = [self mapHitResultsWithSceneResults:results];
NSDictionary *result = getSceneObjectHitResult(resultsMapped, tapPoint);
return result;
}
static NSDictionary * getSceneObjectHitResult(NSMutableArray *resultsMapped, const CGPoint tapPoint) {
return @{
@"results": resultsMapped
};
}
static SCNVector3 toSCNVector3(simd_float4 float4) {
SCNVector3 positionAbsolute = SCNVector3Make(float4.x, float4.y, float4.z);
return positionAbsolute;
}
- (SCNVector3)getRelativePositionToOrigin:(const SCNVector3)positionAbsolute {
SCNVector3 originPosition = self.localOrigin.position;
SCNVector3 position = SCNVector3Make(positionAbsolute.x - originPosition.x, positionAbsolute.y- originPosition.y, positionAbsolute.z - originPosition.z);
return position;
}
- (SCNVector3)getAbsolutePositionToOrigin:(const SCNVector3)positionRelative {
SCNVector3 originPosition = self.localOrigin.position;
SCNVector3 position = SCNVector3Make(positionRelative.x + originPosition.x, positionRelative.y+ originPosition.y, positionRelative.z + originPosition.z);
return position;
}
- (NSMutableArray *) mapHitResultsWithSceneResults: (NSArray<SCNHitTestResult *> *)results {
NSMutableArray *resultsMapped = [NSMutableArray arrayWithCapacity:[results count]];
[results enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
SCNHitTestResult *result = (SCNHitTestResult *) obj;
SCNNode * node = result.node;
NSString * nodeId = [self findNodeId:node];
if(nodeId) {
SCNVector3 positionAbsolute = result.worldCoordinates;
SCNVector3 position = [self getRelativePositionToOrigin:positionAbsolute];
SCNVector3 normal = result.worldNormal;
float distance = [self getCameraDistanceToPoint:positionAbsolute];
NSDictionary *result = @{
@"id": nodeId,
@"distance": @(distance),
@"positionAbsolute": @{
@"x": @(positionAbsolute.x),
@"y": @(positionAbsolute.y),
@"z": @(positionAbsolute.z)
},
@"position": @{
@"x": @(position.x),
@"y": @(position.y),
@"z": @(position.z)
},
@"normal": @{
@"x": @(normal.x),
@"y": @(normal.y),
@"z": @(normal.z)
}
};
[resultsMapped addObject:(result )];
}
}];
return resultsMapped;
}
static id ObjectOrNull(id object)
{
return object ?: [NSNull null];
}
- (NSMutableArray *) mapHitResults:(NSArray<ARHitTestResult *> *)results {
NSMutableArray *resultsMapped = [NSMutableArray arrayWithCapacity:[results count]];
[results enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
ARHitTestResult *result = (ARHitTestResult *) obj;
SCNVector3 positionAbsolute = toSCNVector3(result.worldTransform.columns[3]);
SCNVector3 position = [self getRelativePositionToOrigin:positionAbsolute];
[resultsMapped addObject:(@{
@"distance": @(result.distance),
@"id": ObjectOrNull(result.anchor.identifier.UUIDString),
@"positionAbsolute": @{
@"x": @(positionAbsolute.x),
@"y": @(positionAbsolute.y),
@"z": @(positionAbsolute.z)
},
@"position": @{
@"x": @(position.x),
@"y": @(position.y),
@"z": @(position.z)
}
} )];
}];
return resultsMapped;
}
#pragma mark - node register
- (void)registerNode:(SCNNode *)node withId:(NSString *)nodeId {
[self removeNode:nodeId];
if (node) {
//NSLog(@"registering node %@", nodeId);
[self.nodes setObject:node forKey:nodeId];
// are there any orphans? (3d objects that have a parent, but parent has not yet been mounted
// this seems to be the default case as react mounts first the children
NSSet * orphans = [self.orphans objectForKey:nodeId];
if(orphans) {
for (SCNNode * child in [orphans allObjects]) {
[node addChildNode:child];
}
[self.orphans removeObjectForKey:nodeId];
}
}
}
- (NSString *) findNodeId:(SCNNode *)nodeWithParents {
SCNNode* _node = nodeWithParents;
while(_node) {
if(_node.name && [self.nodes objectForKey:_node.name]) {
return _node.name;
}
_node = _node.parentNode;
}
return nil;
}
- (SCNNode *)getNodeWithId:(NSString *)nodeId {
return [self.nodes objectForKey:nodeId];
}
- (void)removeNode:(NSString *)nodeId {
SCNNode *node = [self getNodeWithId:nodeId];
if (node) {
//NSLog(@"removing node: %@ ", key);
if(node.parentNode) {
if(node.light) {
// see https://stackoverflow.com/questions/47270056/how-to-remove-a-light-with-shadowmode-deferred-in-scenekit-arkit?noredirect=1#comment81491270_47270056
node.hidden = YES;
[node removeFromParentNode];
} else {
[node removeFromParentNode];
}
}
[self.nodes removeObjectForKey:nodeId];
}
}
- (bool)updateNode:(NSString *)nodeId
properties:(NSDictionary *) properties {
SCNNode *node = [self getNodeWithId:nodeId];
//NSLog(@"updating node %@ :%@", nodeId, properties);
if(node) {
[RCTConvert setNodeProperties:node properties:properties];
if(node.geometry && properties[@"shape"]) {
[RCTConvert setShapeProperties:node.geometry properties:properties[@"shape"]];
}
if(properties[@"material"]) {
for (id material in node.geometry.materials) {
[RCTConvert setMaterialProperties:material properties:properties[@"material"]];
}
}
if(node.light) {
[RCTConvert setLightProperties:node.light properties:properties];
}
return true;
} else {
NSLog(@"WARNING: node does not exists: %@. This means that the node has not been mounted yet, so native calls got out of order", nodeId);
return false;
}
}
#pragma mark - RCTARKitSessionDelegate
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame {
simd_float4 pos = frame.camera.transform.columns[3];
self.cameraOrigin.position = SCNVector3Make(pos.x, pos.y, pos.z);
simd_float4 z = frame.camera.transform.columns[2];
self.cameraDirection = SCNVector3Make(-z.x, -z.y, -z.z);
self.cameraOrigin.eulerAngles = SCNVector3Make(0, atan2f(z.x, z.z), 0);
self.frontOfCamera.position = SCNVector3Make(pos.x - focDistance * z.x, pos.y - focDistance * z.y, pos.z - focDistance * z.z);
self.frontOfCamera.eulerAngles = self.cameraOrigin.eulerAngles;
}
- (float)getCameraDistanceToPoint:(SCNVector3)point {
return getDistance(self.cameraOrigin.position, point);
}
static float getDistance(const SCNVector3 pointA, const SCNVector3 pointB) {
float xd = pointB.x - pointA.x;
float yd = pointB.y - pointA.y;
float zd = pointB.z - pointA.z;
float distance = sqrt(xd * xd + yd * yd + zd * zd);
if (distance < 0){
return (distance * -1);
} else {
return (distance);
}
}
@end