旋转一个SCNode对象使用SceneKit三维空间中 [英] Revolve a SCNode object in the 3D Space using SceneKit

查看:3624
本文介绍了旋转一个SCNode对象使用SceneKit三维空间中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解我周围SceneKit方式作为一个例子,我想我会尝试使用SDK建立一个太阳能系统。我能够添加SCNode对象和配置的图案和灯光的材料特性。另外,我能够旋转的行星,但我似乎无法理解我怎么转他们沿着特定路径。

I am trying to learn my way around SceneKit and as an example, I thought I'd try to build a solar system using the SDK. I am able to add SCNode objects and configure its material properties like pattern and lighting. Also, I am able to rotate the planets, but I can't seem to understand how do I "revolve" them along a particular path.

我的code到目前为止是这样的:

My code so far looks like:

// create a new scene
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, 2);

// create and add a 3d sphere - sun to the scene
SCNNode *sphereNode = [SCNNode node];
sphereNode.geometry = [SCNSphere sphereWithRadius:0.3];
[scene.rootNode addChildNode:sphereNode];

// create and configure a material
SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = [NSImage imageNamed:@"SunTexture"];
material.specular.contents = [NSColor whiteColor];
material.specular.intensity = 0.2;
material.locksAmbientWithDiffuse = YES;

// set the material to the 3d object geometry
sphereNode.geometry.firstMaterial = material;

// create and add a 3d sphere - earth to the scene
SCNNode *earthNode = [SCNNode node];
earthNode.geometry = [SCNSphere sphereWithRadius:0.15];
NSLog(@"Sun position = %f, %f, %f", sphereNode.position.x, sphereNode.position.y, sphereNode.position.z);
earthNode.position = SCNVector3Make(0.7, 0.7, 0.7);
NSLog(@"Earth position = %f, %f, %f", earthNode.position.x, earthNode.position.y, earthNode.position.z);
//earthNode.physicsBody = [SCNPhysicsBody dynamicBody];
[scene.rootNode addChildNode:earthNode];

SCNMaterial *earthMaterial = [SCNMaterial material];
earthMaterial.diffuse.contents = [NSImage imageNamed:@"EarthTexture"];

earthNode.geometry.firstMaterial = earthMaterial;

我在您需要添加的行星在太阳的坐标系统,而不是在根节点AppleDocs读的地方,但我不知道如果我的理解是正确的呢。

I read somewhere in AppleDocs that you need to add planets in the Sun's co-ordinate system and not in the root node but I am not sure if I understood that properly anyway.

让我知道你怎么弄了解这样做。

Let me know how do you get about doing this.

推荐答案

SceneKit不提供动画,沿路径API。有几种方法来设置有点像太阳系,虽然。

SceneKit doesn't provide an animate-along-path API. There are several ways to set up something like a solar system, though.

  1. 曾经见过一个太阳系仪?这是在<有点模型href="https://developer.apple.com/library/$p$prelease/mac/documentation/SceneKit/Reference/SCNNode_Class/index.html#//apple_ref/doc/uid/TP40012008">SCNNode文档都在谈论;这也是什么<一个href="https://developer.apple.com/library/$p$prelease/mac/sample$c$c/SceneKitWWDC2014/Introduction/Intro.html#//apple_ref/doc/uid/TP40014551">WWDC presentation 确实在场景图汇总的幻灯片。诀窍在于,你用一个节点重新present的围绕太阳的行星的公转参照系 - 这样拥有行星在太阳系仪电枢,如果你把这个节点围绕其中心轴,任何一个孩子转您可以将它节点将遵循圆形路径。有关code表示这是 ASCSceneGraphSummary.m 在样品code项目(凝聚只显示节点层次设置在这里):

  1. Ever seen an orrery? This is the model that bit in the SCNNode documentation is talking about; it's also what the WWDC presentation does on the "Scene Graph Summary" slide. The trick is that you use one node to represent the frame of reference for a planet's revolution around the sun — like the armature that holds a planet in an orrery, if you make this node rotate around its central axis, any child node you attach to it will follow a circular path. The relevant code for this is in ASCSceneGraphSummary.m in that sample code project (condensed to just show node hierarchy setup here):

// Sun
_sunNode = [SCNNode node];
_sunNode.position = SCNVector3Make(0, 30, 0);
[self.contentNode addChildNode:_sunNode];

// Earth-rotation (center of rotation of the Earth around the Sun)
SCNNode *earthRotationNode = [SCNNode node];
[_sunNode addChildNode:earthRotationNode];

// Earth-group (will contain the Earth, and the Moon)
_earthGroupNode = [SCNNode node];
_earthGroupNode.position = SCNVector3Make(15, 0, 0);
[earthRotationNode addChildNode:_earthGroupNode];

// Earth
_earthNode = [_wireframeBoxNode copy];
_earthNode.position = SCNVector3Make(0, 0, 0);
[_earthGroupNode addChildNode:_earthNode];

// Moon-rotation (center of rotation of the Moon around the Earth)
SCNNode *moonRotationNode = [SCNNode node];
[_earthGroupNode addChildNode:moonRotationNode];

// Moon
_moonNode = [_wireframeBoxNode copy];
_moonNode.position = SCNVector3Make(5, 0, 0);
[moonRotationNode addChildNode:_moonNode];

  • 您不能让下面你在3D空间模拟特定的路径动画,但你可以做一个关键帧动画,在3D空间中的多个位置之间进行插值。的(未经测试)code以下走动正方形在XZ平面节点......添加更多的积分以获得圆润的形状。

  • You can't make an animation that follows a specific path you model in 3D space, but you could make a keyframe animation that interpolates between several positions in 3D space. The (untested) code below moves a node around a square shape in the xz-plane... add more points to get a rounder shape.

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPaths:@"position"];
    anim.values = @[
        [NSValue valueWithSCNVector3:SCNVector3Make(0, 0, 1)],
        [NSValue valueWithSCNVector3:SCNVector3Make(1, 0, 0)],
        [NSValue valueWithSCNVector3:SCNVector3Make(0, 0, -1)],
        [NSValue valueWithSCNVector3:SCNVector3Make(-1, 0, 0)],
    ];
    [node addAnimation:anim forKey:"orbit"];
    

  • 使用物理!该<一href="https://developer.apple.com/library/$p$prelease/mac/documentation/SceneKit/Reference/SCNPhysicsField_Class/index.html#//apple_ref/occ/cl/SCNPhysicsField"><$c$c>SCNPhysicsField级车型径向重力,所以你能把一个重力场到场景中,添加一些物理机构的行星,并坐下来,看着你的太阳系<打击>大灾变摧毁本身来的生活!这里获得现实的行为需要大量的试验和错误 - 你需要设置一个初始速度为每个行星切到你所期望它的轨道是,和调整的速度得到它的权利。一些研究的星历数据为我们的太阳系可能的帮助。

  • Use physics! The SCNPhysicsField class models radial gravity, so you can just drop a gravity field into a scene, add some physics bodies for planets, and sit back and watch your solar system cataclysmically destroy itself come to life! Getting realistic behavior here takes a lot of trial and error — you'll need to set an initial velocity for each planet tangential to what you expect its orbit to be, and tweak that velocity to get it right. Researching some ephemeris data for our solar system might help.

    这篇关于旋转一个SCNode对象使用SceneKit三维空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆