
同じ大きさの箱の絵が奥と手前で違う大きさ?なiPhoneアプリのサンプルコード
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic, weak) SCNView *sv2;
@property (nonatomic, weak) SCNNode *selected;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createTable];
[self createCamera];
[self createScene2];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor lightGrayColor];
sv.scene = [SCNScene scene];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createTable {
SCNBox *table = [SCNBox boxWithWidth:120 height:1 length:25 chamferRadius:0];
table.firstMaterial.diffuse.contents = [UIColor brownColor];
SCNNode *tableNode = [SCNNode nodeWithGeometry:table];
[self.sceneView.scene.rootNode addChildNode:tableNode];
tableNode.physicsBody = [SCNPhysicsBody staticBody];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 5, 20);
camera.transform = SCNMatrix4Mult(camera.transform,SCNMatrix4Mult(SCNMatrix4MakeRotation(-0.5, 1, 0, 0), SCNMatrix4MakeRotation(-1.0, 0, 1, 0)));
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createScene2 {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [UIColor clearColor];
[self.view addSubview:sv];
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.usesOrthographicProjection = YES;
camera.camera.orthographicScale = 10.0;
camera.position = SCNVector3Make(0, 0, 20);
[sv.scene.rootNode addChildNode:camera];
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeOmni;
SCNNode *lightNode = [SCNNode node];
lightNode.position = SCNVector3Make(50, 50, 200);
lightNode.light = light;
[sv.scene.rootNode addChildNode:lightNode];
self.sv2 = sv;
for (int i=0; i<2; i++) {
SCNBox *box = [SCNBox boxWithWidth:4 height:2 length:4 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor orangeColor];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = @”box”;
boxNode.position = SCNVector3Make(-10 + 20 * i, 2, 0);
boxNode.rotation = SCNVector4Make(1, 1, 0, 0.5);
[self.sv2.scene.rootNode addChildNode:boxNode];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.sv2];
SCNHitTestResult *hit = [self.sv2 hitTest:p options:@{SCNHitTestSortResultsKey : @YES}].firstObject;
if ([hit.node.name isEqual:@”box”]) {
self.selected = hit.node;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.sv2];
SCNVector3 projectedOrigin = [self.sv2 projectPoint:SCNVector3Zero];
self.selected.position = [self.sv2 unprojectPoint:SCNVector3Make(p.x, p.y, projectedOrigin.z)];
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.selected = nil;
}
@end