How to Move a Node towards camera by certain amount - SceneKit - ios

I'm rendering a 3d model(car) using SceneKit.
Whenever user taps on a car, using SCNHitTestResult i'm trying to add annotation(a simple plane node).
//hit results are obtained using [sceneView hitTest:location options:nil]
SCNHitTestResult * result = hitResults[0];
if([result.node.name isEqualToString:#"Annotation"]){
[result.node removeFromParentNode];
return;
}
SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = [UIImage imageNamed:#"bug"];
SCNPlane *plane = [[SCNPlane alloc] init];
plane.width = 1.0;
plane.height = 1.0;
plane.cornerRadius = 0.5;
plane.firstMaterial = material;
SCNNode *planeNode = [[SCNNode alloc] init];
planeNode.position = result.worldCoordinates;
// planeNode.transform = cameraNode.transform;
planeNode.geometry = plane;
SCNBillboardConstraint *bbc = [[SCNBillboardConstraint alloc] init];
planeNode.constraints = #[bbc];
planeNode.name = #"Annotation";
[scene.rootNode addChildNode:planeNode];
Now the problem is, half of the plane(plane node added as annotation) gets hidden in the intersected node itself.
How can i prevent that... i dont want plane to be hidden. Ideally it should be circular plane. But half of it is missing. I'm thinking of moving it little bit towards the camera. Any help or suggestions will be helpful.

Related

SceneKit transparent node, transparent material

I try to create two node, one with transparent and one is nomal. There're same position nodes.
Code like following
//UIBezierPath *aPath = ....
// add transparent node
SCNMaterial *transparentMaterial = [SCNMaterial material];
UIImage *checkImage = [UIImage imageNamed:#"tree.png"];
transparentMaterial.diffuse.contents = checkImage;
SCNShape *transparentGeometry = [SCNShape shapeWithPath:aPath extrusionDepth:0.000001];
transparentMaterial.transparency = 0.000001;
transparentGeometry.materials = #[transparentMaterial];
SCNNode *transparentNode = [SCNNode new];
transparentNode.geometry = transparentGeometry;
transparentNode.transform = SCNMatrix4MakeRotation(M_PI_2, 1, 0, 0);
[root addChildNode:transparentNode];
// addd node
SCNMaterial *materal = [SCNMaterial material];
UIImage *checkImage1 = [UIImage imageNamed:#"tree.png"];
materal.diffuse.contents = checkImage1;
SCNShape *geometry = [SCNShape shapeWithPath:aPath extrusionDepth:0.00001];
geometry.materials = #[materal];
SCNNode *node = [SCNNode new];
node.geometry = geometry;
node.transform = SCNMatrix4Rotate(SCNMatrix4MakeRotation(M_PI_2, 1, 0, 0), M_PI_2 , 0, 0, 1);
[root addChildNode:node];
the problem is I can look through visible node via transparent node. Problem only occurred when visible node has material with image that has alpha channel inside (like my example image, it's has clear background).
it's bug of SceneKit? any help would be appreciated!
here is resource of image "tree.png"

iOS+SceneKit: How to apply toon shader on texture?

I want to use a toon-shader with SceneKit on iOS8+ to render a sphere with an Earth texture on it. I also want to add a toon shader for rendering the Earth. So far the shader works on the lighting, but the texture isn't shaded by the toon-shader (cp. to the image below, the texture should also be "tooned").
Someone any ideas?
Here's my view controller code (self.sceneKitView is an instance of SCNView):
#implementation ToonViewController
- (void)viewDidLoad {
[super viewDidLoad];
SCNScene *scene = [SCNScene scene];
// 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, 15);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// 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 darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
// set up the scene
self.sceneKitView.scene = scene;
self.sceneKitView.allowsCameraControl = YES;
self.sceneKitView.showsStatistics = NO;
self.sceneKitView.backgroundColor = [UIColor clearColor];
NSMutableDictionary* shaders = [[NSMutableDictionary alloc] init];
shaders[SCNShaderModifierEntryPointLightingModel] = [[NSString alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"fixed_toon" withExtension:#"shader"]
encoding:NSUTF8StringEncoding
error:nil];
SCNNode* earth = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:5.0]];
earth.position = SCNVector3Make(0.0, 0.0, 0.0);
[scene.rootNode addChildNode:earth];
[earth runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0.0 y:0.25 z:0.0 duration:1.0]]];
SCNMaterial* earthMaterial = [SCNMaterial material];
earthMaterial.diffuse.contents = [UIImage imageNamed:#"Earth.png"];
earthMaterial.specular.contents = [UIColor whiteColor];
earthMaterial.specular.intensity = 0.2;
earthMaterial.locksAmbientWithDiffuse = NO;
earthMaterial.shaderModifiers = shaders;
earth.geometry.firstMaterial = earthMaterial;
}
#end
And this is the fixed_toon.shader file:
vec3 lDir = normalize(vec3(0.1, 1.0, 1.0));
float dotProduct = dot(_surface.normal, lDir);
_lightingContribution.diffuse += (dotProduct*dotProduct*_light.intensity.rgb);
_lightingContribution.diffuse = floor(_lightingContribution.diffuse*4.0)/3.0;
vec3 halfVector = normalize(lDir + _surface.view);
dotProduct = max(0.0, pow(max(0.0, dot(_surface.normal, halfVector)), _surface.shininess));
dotProduct = floor(dotProduct*3.0)/3.0;
//_lightingContribution.specular += (dotProduct*_light.intensity.rgb);
_lightingContribution.specular = vec3(0,0,0);
The shader you have is only a lighting shader. If you want the earth to be tooned, you need a fragment shader.
Look at the shader file all of the math is related to light. You need something that will adjust the pixel color of the texture. Then connect it using the SCNShaderModifierEntryPointFragment entry point.

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.

How to make a sprite which has physicsBody unmovable/static upon collision?

I have a game with balloons. I am adding the balloons from the bottom and they fly up while able to fly offscreen. I gave the balloons physicsBodys so they collide which each other but I want to make the balloons bouce of the left and right edge of the screen. I found a solution where the sprite object bounce of all screens on Rays site. But I need a different solution.
This is my balloon code:
MBDBallon *randomBallon = [[MBDBallon alloc] initWithImageName:balloonImageName
andBallonSize:ballonSize
withBallonImageName:[self.balloonColorImageNamesArray objectAtIndex:balloonWithColorIndex]
withStartingPosition:CGPointMake(self.viewSize.width/2, - self.viewSize.height )];
//randomBallon.name = [self returnRandomImageNameFromArray:self.ballonSpriteImageNamesArray];
randomBallon.physicsBody.usesPreciseCollisionDetection = YES;
randomBallon.physicsBody.categoryBitMask = balloonCategory;
randomBallon.physicsBody.collisionBitMask = balloonCategory;
randomBallon.physicsBody.contactTestBitMask = balloonCategory;
and this is my left wall code:
SKSpriteNode *leftWall = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeMake( self.viewSize.width / 15, self.viewSize.height * 3 )];
[leftWall setAnchorPoint:CGPointMake(0.0f, 0.0f)];
leftWall.position = CGPointMake( self.viewSize.width / 15 , 0.0f );
leftWall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake( self.viewSize.width / 15, self.viewSize.height * 3 )];
leftWall.physicsBody.categoryBitMask = balloonCategory;
leftWall.physicsBody.collisionBitMask = balloonCategory;
leftWall.physicsBody.contactTestBitMask = balloonCategory;
leftWall.physicsBody.usesPreciseCollisionDetection = YES;
leftWall.physicsBody.allowsRotation = NO;
leftWall.physicsBody.dynamic = NO;
leftWall.physicsBody.friction = 0.0f;
[self addChild:leftWall];
However the walls is moving after some few balloon collisions. I really don't know wheres the catch or property to stop such behavior. The :
leftWall.physicsBody.dynamic = NO;
Should stop all movement but it doesn't. Please help, thanks :).
You have not properly set up your categoryBitMasks.
randomBallon.physicsBody.categoryBitMask = balloonCategory;
randomBallon.physicsBody.collisionBitMask = wallCategory|balloonCategory;
leftWall.physicsBody.collisionBitMask = wallCategory;
leftWall.physicsBody.contactTestBitMask = balloonCategory;
CategoryBitMask is the category you want to assign to your sprite. CollisionBitMask is the category you want your sprite to collide with. Balloons in this case should be set to collide with walls and other balloons. Walls should be set to only collide with balloons.

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