ARKit-动画

96
Heikki_
2017.08.07 00:15* 字数 31

在ARKit中实现动画

一,SCNAction

/**
* 绕自身y轴旋转
repeatActionForever 为持续执行action
*/
 [self.mercuryNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
1.gif
/**
* 缩放
*/
 [self.mercuryNode runAction:[SCNAction scaleTo:0.1 duration:10]];
缩放.gif
/**
* 平移
*/
[self.mercuryNode runAction:[SCNAction moveBy:SCNVector3Make(2, 0,0) duration:10]];

平移.gif

/*! 增加完成回调
 @method runAction:completionHandler:
 @abstract Adds an action to the list of actions executed by the node. Your block is called when the action completes.
 */
- (void)runAction:(SCNAction *)action completionHandler:(nullable void (^)())block NS_AVAILABLE(10_10, 8_0);
/**
*旋转
*/
//Creates an action that rotates the node by a relative value.
+ (SCNAction *)rotateByX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;
//Creates an action that rotates the node to an absolute angle.
+ (SCNAction *)rotateToX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;
+ (SCNAction *)rotateToX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration shortestUnitArc:(BOOL)shortestUnitArc;
//Creates an action that rotates the node arond an axis by the specified angle
+ (SCNAction *)rotateByAngle:(CGFloat)angle aroundAxis:(SCNVector3)axis duration:(NSTimeInterval)duration;
+ (SCNAction *)rotateToAxisAngle:(SCNVector4)axisAngle duration:(NSTimeInterval)duration;
/**
*缩放
*/
//Creates an action that changes the x, y and z scale values of a node by a relative value.
+ (SCNAction *)scaleBy:(CGFloat)scale duration:(NSTimeInterval)sec;
//Creates an action that changes the x, y and z scale values of a node.
+ (SCNAction *)scaleTo:(CGFloat)scale duration:(NSTimeInterval)sec;
/**
*平移
*/
//Creates an action that moves a node relative to its current position.
+ (SCNAction *)moveByX:(CGFloat)deltaX y:(CGFloat)deltaY z:(CGFloat)deltaZ duration:(NSTimeInterval)duration;
+ (SCNAction *)moveBy:(SCNVector3)delta duration:(NSTimeInterval)duration;
//Creates an action that moves a node to a new position.
+ (SCNAction *)moveTo:(SCNVector3)location duration:(NSTimeInterval)duration;

二,核心动画

/**
* 物体自传 绕自身轴心
*/
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contentsTransform"];
    animation.duration = 10.0;
    animation.repeatCount = FLT_MAX;
    SCNMatrix4 matrix4F = SCNMatrix4Mult(SCNMatrix4MakeTranslation(0, 0, 0), SCNMatrix4MakeScale(3, 3, 3));
    SCNMatrix4 matrix4T = SCNMatrix4Mult(SCNMatrix4MakeTranslation(1, 0, 0), SCNMatrix4MakeScale(3, 3, 3));
    animation.fromValue = [NSValue valueWithSCNMatrix4:matrix4F];
    animation.toValue = [NSValue valueWithSCNMatrix4:matrix4T];
    [_sunNode.geometry.firstMaterial.diffuse addAnimation:animation forKey:@"sun-texture"];

  //此处实现的是node 材质的动画,使材质旋转起来,有node 在旋转的感觉
自转.gif
/**
*物体公转 绕某外部轴
*/
    SCNNode *node1 = [[SCNNode alloc]init];
    node1.position = self.arSCNView.scene.rootNode.position;
    [self.arSCNView.scene.rootNode addChildNode:node1];
    [node1 addChildNode:self.planeNode];
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"rotation"];
    rotationAnimation.duration = 20;
    rotationAnimation.repeatCount = FLT_MAX;
    rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 1, 0, M_PI * 2)];
    [node1 addAnimation:rotationAnimation forKey:@"moon rotation around earth"];
    
//需先将想要旋转的node add 到一个空node ,再将核心动画加到空node 上
//直接加到node上 即自传
公转.gif

整理的一些ARKit 学习资料
http://www.jianshu.com/p/f854246ec690

iOS开发
Web note ad 1