Sceneview Camera Node full screen - ios

I'm trying to set the size of the camera node to be the same as the scene view. I've managed to set the scene view to fit the full screen, this is been proven by setting the background colour of this. This was achieved by the code below:
CGRect aFrame = self.sceneView.frame;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
aFrame.size.width = screenWidth;
aFrame.size.height = screenHeight;
self.sceneView.frame = aFrame;
However when I initialise my camera node it doesn't seem to fill the scene view and I don't know how to access and set its size so the camera fills the whole screen. I'm fairly new to objective-c so all help is appreciated.
The code for the camera node can be found below:
- (nonnull SCNScene *)createScene {
SCNScene *scene = [SCNScene scene];
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 10);
[scene.rootNode addChildNode:cameraNode];
_cameraNode = cameraNode;
_trackableNodes = [NSMutableArray new];
return scene;
}

Related

SK3DNode not rotating

I have a 3D sphere model saved as a dae file and loaded to xcode.
Problem is I cannot get it spinning/rotating - it just stays static... I've tried several ways, even tried to get the camera to move around it.
SCNScene *model = [SCNScene sceneNamed:#"model3.dae"];
SK3DNode *node = [[SK3DNode alloc] initWithViewportSize:CGSizeMake(50, 50)];
//workaround
id s1 = [node valueForKey:#"_scnRenderer"];
NSLog(#"%#", s1);
node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:node.frame.size.width/2];
node.physicsBody.affectedByGravity = NO;
node.physicsBody.allowsRotation = YES;
node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[node setScnScene:model];
//THIS IS THE PART! I cannot get it to spin around
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:#"rotation"];
rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(1, 2, 3, M_PI * 2)];
rotationAnimation.duration = 1;
rotationAnimation.repeatCount = FLT_MAX;
[model.rootNode addAnimation:rotationAnimation forKey:nil];
//camera
SCNCamera *camera = [SCNCamera camera];
camera.xFov = 0;
camera.yFov = 0;
camera.zNear = 0.0;
camera.zFar = 10.0;
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = camera;
cameraNode.position = SCNVector3Make(0, 0, 5);
[model.rootNode addChildNode:cameraNode];
node.pointOfView = cameraNode;
[world addChild:node];
Any ideas? Basically I want to try get the 3D model moving around inside the SK3DNode. Hope this makes sense. Think spinning planet.
Thanks in advance,
Alex
modifying the transform (by adding an animation, for instance) of the root node of a scene has no effect because the camera is held by a child node of that root node. The relative transform between the sphere and the camera stays the same. You should add an animation to the camera node or to the sphere node instead.

Objective-C. How is your first SKScene initialised?

This method sets up the initial scene and is called as soon as the application opens:
#import "GameScene.h"
#import "WarScene.h"
#import "Math.h"
#implementation GameScene
{
SKNode *map;
float oldY;
float oldX;
BOOL buttonClicked;
int thisButton;
float oldMapPosition;
float midScreenX;
float midScreenY;
int separation;
float sendY;
BOOL interacting;
CGVector speed;
float oldPosition;
float newPosition;
BOOL isReleased;
int iterations;
float diff;
BOOL comeToStop;
BOOL actualStop;
BOOL clicked;
}
int numberOfLevels = 20;
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
map = [SKNode node];
separation = [UIScreen mainScreen].bounds.size.height;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
//landscape mode
midScreenX = ([[UIScreen mainScreen]bounds].size.width>[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
midScreenY = ([[UIScreen mainScreen]bounds].size.width<[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
}
else
{
//portrait mode
midScreenX = [[UIScreen mainScreen]bounds].size.width/2;
midScreenY = [[UIScreen mainScreen]bounds].size.height/2;
}
NSLog(#"Initial Width: %f Height: %f", midScreenX*2, midScreenY*2);
map.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: CGSizeMake([UIScreen mainScreen].bounds.size.width, separation*(numberOfLevels+1))];
map.physicsBody.affectedByGravity = NO;
map.physicsBody.allowsRotation = NO;
clicked = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
self.backgroundColor = [SKColor blackColor];
for (int i = 0; i<numberOfLevels; i += 1) {
SKLabelNode *title;
{
title = [SKLabelNode labelNodeWithFontNamed:#"Cochin"];
title.text = [NSString stringWithFormat: #"Level %d", 1+i];
title.fontSize = 60;
title.position = CGPointMake(CGRectGetMidX(self.frame),
((i+1)*separation));
title.fontColor = [UIColor whiteColor];
title.name = [NSString stringWithFormat: #"Level %d", 1+i];
[map addChild:title];
}
}
[self addChild:map];
}
This works exactly as I intended it to, however, when I call this from another class:
-(void)continueMethod:(UIButton*)button{
NSLog(#"InitwithSize to GameScene Width: %f Height: %f", midScreenX*2, midScreenY*2);
GameScene *map = [[GameScene alloc] initWithSize: CGSizeMake(midScreenX*2 ,midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:3];
[viewToRemove removeFromSuperview];
[skView presentScene:map transition:reveal];
}
The scene which should be set up doesn't appear as I intended it to, or indeed how it used to look the last time that it was initialised. The separation variable appears bigger, the text appears bigger and everything is wider. I've verified that the UIScreen that I have initialised is exactly the same throughout.
This led me to question how the initial scene of a SKSprite application comes to be and whether it is different to "initWithSize:" however, looking through all the code that comes in the game template I can't ever see my first scene get called. The closest that I can find to initWithSize: is the following from the GameViewController-
GameScene *scene = [GameScene unarchiveFromFile:#"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
Firstly, is this what is initialising my initial scene? Secondly is it in anyway different to initWithSize: ? And finally if the answer to the previous tow questions is yes, should it be used instead of initWithsize: ?
Yes, this is the initialization of the initial scene! What you are seeing in the View Controller is a slightly different initialization. You are using the sks file, first of all, and initWithCoder: is being called when the view controller unarchives it. However, this isn't very different from initWithSize if you specify all of the same dimensions and properties.
That being said, the scene's scaleMode in your initialization and the view controller's scaleMode is different. As you can see, the view controller specifies:
scene.scaleMode = SKSceneScaleModeAspectFill;
The AspectFill scale mode according to Apple:
The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible but may require letterboxing in the view.
When you initialize your scene, on the other hand, you leave the default scaleMode, which happens to be SKSceneScaleModeFill. This maps each axis of the scene to the view, so you get a distorted scene. What you need to do is simply state:
map.scaleMode = SKSceneScaleModeAspectFill;
This will use the same scale mode as in the original scene and the distortion that you see now will be gone.
Hope this helps, let me know if you have questions.

Setting SCNNode's rotation with valid values, but it's always 0,0,0,0

I'm trying to set my camera node's rotation, and the value's are there, but it never changes from 0,0,0,0...
initialize the player node (left out other settings, the node has no geometry, but its physics body does have geometry)
playerNode = [SCNNode node];
and I set it's position and add it to the scene's root node...
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
SCNScene *scene = [SCNScene scene];
scene.physicsWorld.gravity = SCNVector3Make(0, -9, 0);
scene.physicsWorld.timeStep = 1.0/360;
// add world node
worldNode = [SCNNode node];
worldNode.name = #"world";
[scene.rootNode addChildNode:worldNode];
// add terrain
.../* terrain stuff */
// add player node
playerNode = [SCNNode node];
playerNode.name = #"player";
playerNode.position = SCNVector3Make(0, 0, 0);
playerNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:[SCNPhysicsShape shapeWithGeometry:[SCNCylinder cylinderWithRadius:0.2 height:1] options:nil]];
playerNode.physicsBody.angularDamping = 0.9999;
playerNode.physicsBody.damping = 0.9999;
playerNode.physicsBody.rollingFriction = 0;
playerNode.physicsBody.friction = 0;
playerNode.physicsBody.restitution = 0;
playerNode.physicsBody.velocityFactor = SCNVector3Make(1, 0, 1);
playerNode.physicsBody.categoryBitMask = playerCategory;
[scene.rootNode addChildNode:playerNode];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
[playerNode addChildNode:cameraNode];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.xFov = 53;
cameraNode.camera.zNear = 0.01;
cameraNode.camera.zFar = 5000;
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 0);
.../* rest of view did load */
}
and then trying to set the rotation:
-(void)lookGestureRecognized:(UIPanGestureRecognizer *)gesture {
SCNView *scnView = (SCNView *)self.view;
CGPoint translation = [gesture translationInView:self.view];
NSLog(#"lookGestureRecognized: translation x %g y %g", translation.x, translation.y);
CGFloat hAngle = acos(((float)translation.x / 200) - (float)M_PI_2);
CGFloat vAngle = acos(((float)translation.y / 200) - (float)M_PI_2);
// rotate hero
[playerNode.physicsBody applyTorque:SCNVector4Make(0, 1, 0, hAngle) impulse:YES];
// tilt camera
[SCNTransaction setAnimationDuration:0.0];
elevation = MAX((float)-M_PI_4, MIN((float)M_PI_4, elevation + vAngle));
NSLog(#"elevation: %g", elevation);
SCNVector4 cameraRotation = SCNVector4Make(1, 0, 0, elevation);
cameraNode.rotation = cameraRotation;
// cameraNode.transform = SCNMatrix4Rotate(cameraNode.transform, 1, 0, 0, elevation); // tried this, didn't work either
NSLog(#"cameraNode.rotation = x %g y %g z %g, w %g", cameraNode.rotation.x, cameraNode.rotation.y, cameraNode.rotation.z, cameraNode.rotation.w);
// reset translation
[gesture setTranslation:CGPointZero inView:self.view];
}
the elevation is being calculated correctly, but trying to set it to the node's rotation fails... the log always says 0,0,0,0...
NSLog sample output:
2015-02-17 14:37:11.732 usingGestureRecognizer.01[96111:289778] lookGestureRecognized: translation x 0 y 0.5
2015-02-17 14:37:11.733 usingGestureRecognizer.01[96111:289778] elevation: -0.785398
2015-02-17 14:37:11.733 usingGestureRecognizer.01[96111:289778] cameraNode.rotation = x 0 y 0 z 0, w 0
Any ideas?
(Side note, mimic'ing the code I found from an example, translated it from swift to obj-c, and the sample code works perfectly)
The simple answer? I'm a newb.
The actual answer? Be very careful when copying code from a tutorial... I had declared a private variable SCCNode cameraNode, and was trying to change this node's rotation, however, the actual camera node doing the camera work was declared as a private method variable in viewDidLoad...
So, again, thank you very much for your help guys, this site is invaluable to me, I have learned so much, but I'm personally 0 for 2 asking questions, both of mine have just been MY oversights. Thanks again, sincerely

scenekit model from sketchup disappears

I am playing around with scene kit.
I do not know a lot about modeling but i created a very basic Sketchup model with no materials or textures.
I exported the model from ketchup and imported it into the scene.
As i move the camera around towards one end of the model the rear disappears and vice versa. As the camera rotates towards the front the rear gets shadowed in black.
What is the reason for this?
Lighting? Here is the code for the scene. Its a basic Xcode game project
// create a new scene
SCNScene *scene = [SCNScene sceneNamed:#"ship_transport.dae"];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 100);
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient
ambientLightNode.light.color = [UIColor whiteColor];
[scene.rootNode addChildNode:ambientLightNode];
// retrieve the ship node
SCNNode *ship = [scene.rootNode childNodeWithName:#"ship" recursively:YES];
ship.geometry.firstMaterial.diffuse.contents = [UIColor redColor];
ship.geometry.firstMaterial.specular.contents = [UIColor whiteColor];
SCNVector3 vector = SCNVector3Make(.1, .1, .1);
[ship setScale:vector];
//[scene.rootNode addChildNode:lightNode];
// animate the 3d object
[ship runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:0 duration:1]]];
// retrieve the SCNView
SCNView *scnView = (SCNView *)self.view;
scnView.autoenablesDefaultLighting = true;
// set the scene to the view
scnView.scene = scene;
// allows the user to manipulate the camera
scnView.allowsCameraControl = YES;
// show statistics such as fps and timing information
scnView.showsStatistics = YES;
// configure the view
scnView.backgroundColor = [UIColor darkGrayColor];
Try setting the zFar property on the camera. Other related properties to try changing would be the xFov and yFov.

How to change anchor point?

I'm working with SceneKit. The object SCNNode is rotated relative to the center. How to change anchor point of the SCNNode?
SCNScene *scene = [SCNScene new];
SCNBox *boxGeometry = [SCNBox boxWithWidth:384.f height:512.f length:20.f chamferRadius:0];
SCNMaterial *material = [SCNMaterial new];
material.diffuse.contents = [UIImage imageNamed:#"material"];
SCNNode *boxNode = [SCNNode nodeWithGeometry:boxGeometry];
boxNode.geometry.firstMaterial = material;
boxNode.pivot = SCNMatrix4MakeTranslation(0.5f, 0.5f, 0.5f);
[scene.rootNode addChildNode:boxNode];
self.sceneView.scene = scene;
self.sceneView.allowsCameraControl = YES;
Your pivot transform translation is very small compared to the size of your box. It's not normalized.
If you want to translate around one of the corners you should translate half of the side in all directions.
boxNode.pivot = SCNMatrix4MakeTranslation(384.0/2.0, 512.0/2.0, 20.0/2.0);
As a simple example :
yourSceneNode.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5),
where you change the node's position, rotation and scale properties.
SCNNode class reference explains this nicely

Resources