在SpriteKit项目中,我开始使用一些3D对象来获得视觉效果.使用.scn文件将场景文件放到屏幕上非常容易SK3DNode
,但是我一直无法触摸到场景中的3D对象,因此我可以相应地处理它们.
我有一个SK3DNode子类,通过静态方法实例化.initialize方法不执行与SK3DNode属性相关的任何操作:
SceneKitManager *sceneKitManager = [SceneKitManager nodeWithViewportSize:sprite.size]; [sceneKitManager setPosition: sprite.position]; [sceneKitManager setPlaying:YES]; [sceneKitManager setScnScene:[SCNScene sceneNamed:@"file.scn"]]; [sceneKitManager setUserInteractionEnabled:YES]; SCNNode *pov = [sceneKitManager.scnScene.rootNode childNodeWithName:@"camera" recursively:NO]; [sceneKitManager setPointOfView:pov]; [sceneKitManager initialize]; return sceneKitManager;
然后,在同一个子类中,我有touchesBegan方法,它尝试从3D场景取消投影点并使其在SpriteKit的2D坐标系中可用.但是,我的screenCoordinates变量始终为零:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event { NSLog(@"TOUCHED"); UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInNode:self]; vector_float3 touchVector = {touchLocation.x, touchLocation.y, 0.5}; SCNVector3 unprojectedVector = SCNVector3FromFloat3([self unprojectPoint:touchVector]); NSArray *result = [self hitTest:CGPointMake(unprojectedVector.x, unprojectedVector.y) options:NULL]; if([result count] > 0) { SCNHitTestResult *hitResult = [result firstObject]; [hitResult.node removeFromParentNode]; } }
任何帮助当然值得赞赏,如果我遗漏了任何东西,我可以提供更多细节或背景.