第一步:创建一个AR项目 Augumented Reality App 命名ARDemo 如:图-1
第二部:选好图片资源 如图 - 2
第三部:创建节点
//创建节点 封装一个创建节点的方法及太阳系中的各个星球
func createPlaent(radius:Float,image:String) ->SCNNode{
//SCNSphere 球状 materials渲染
letplanet =SCNSphere.init(radius:CGFloat(radius))
letmaterial =SCNMaterial()
material.diffuse.contents=UIImage.init(named:"\(image).jng")
planet.materials= [material]
letplantNode =SCNNode.init(geometry: planet)
returnplantNode
}
第四部:创件节点自转方法
func rotateObject(rotation:Float,planet:SCNNode,duration:Float) ->Void{
letrotation =SCNAction.rotateBy(x:0, y:CGFloat(rotation), z:0, duration:TimeInterval(duration))
planet.runAction(SCNAction.repeatForever(rotation))
}
第五部:创件星球轨道圆环
func createRings(ringSize:Float) ->SCNNode{
letring=SCNTorus.init(ringRadius:CGFloat(ringSize), pipeRadius:0.002)
letmaterial =SCNMaterial()
material.diffuse.contents=UIColor.darkGray
ring.materials= [material]
letringNode =SCNNode.init(geometry: ring)
returnringNode
}
第六步:设置根节点
/// 根节点
letbaseNode =SCNNode()
第七部:实现
//viewDidLoad方法
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate=self
sceneView.showsStatistics=true
//创建太阳
createSCNNode()
}
/// 创建UI
func createSCNNode(){
letscence =SCNScene()
//创建node
letsun=createPlaent(radius:0.25, image:"sun")
letmercury=createPlaent(radius:0.03, image:"mercury")
letvenus =createPlaent(radius:0.04, image:"venus")
letearth =createPlaent(radius:0.05, image:"earth")
letmars =createPlaent(radius:0.03, image:"mars")
letjupiter =createPlaent(radius:0.12, image:"jupiter")
letsaturn =createPlaent(radius:0.09, image:"saturn")
leturanus =createPlaent(radius:0.07, image:"uranus")
letneptune =createPlaent(radius:0.08, image:"neptune")
letmoon=createPlaent(radius:0.01, image:"moon")
letmoonRing =SCNTorus(ringRadius:0.08, pipeRadius:0.000001)
letmoonRingNode =SCNNode(geometry: moonRing)
moon.position=SCNVector3.init(0.08,0,0)
moonRingNode.position=SCNVector3.init(0,0.02,0)
moonRingNode.addChildNode(moon)
letsttuenLoop =SCNBox.init(width:0.4, height:0, length:0.5, chamferRadius:0)
letmaterial =SCNMaterial()
material.diffuse.contents=UIImage.init(named:"saturn_loop.jpg")
sttuenLoop.materials= [material];
letloopNode =SCNNode.init(geometry: sttuenLoop)
loopNode.rotation=SCNVector4.init(-0.5,-0.5,0,5)
loopNode.position=SCNVector3.init(0,0,0)
//创建圆环
letmercuryRing =createRings(ringSize:0.3)
letvenusRing =createRings(ringSize:0.5)
letearthRing =createRings(ringSize:0.7)
letmarsRing =createRings(ringSize:0.8)
letjupiterRing =createRings(ringSize:1)
letsaturnRing =createRings(ringSize:1.25)
leturanusRing =createRings(ringSize:1.5)
letneptuneRing =createRings(ringSize:1.7)
sun.position=SCNVector3.init(0,0,0)
mercury.position=SCNVector3.init(0.3,0,0)
venus.position=SCNVector3.init(0.5,0,0)
earth.position=SCNVector3.init(0.7,0,0)
moon.position=SCNVector3.init(0.1,0,0)
mars.position=SCNVector3.init(0.8,0,0)
jupiter.position=SCNVector3.init(1,0,0)
saturn.position=SCNVector3.init(1.25,0,0)
uranus.position=SCNVector3(x:1.5,y:0,z:0)
neptune.position=SCNVector3(x:1.7,y:0,z:0)
//圆环添加
mercuryRing.addChildNode(mercury)
venusRing.addChildNode(venus)
earthRing.addChildNode(earth)
earth.addChildNode(moonRingNode)
marsRing.addChildNode(mars)
jupiterRing.addChildNode(jupiter)
saturnRing.addChildNode(saturn)
saturn.addChildNode(loopNode)
uranusRing.addChildNode(uranus)
neptuneRing.addChildNode(neptune)
//baseNode
baseNode.addChildNode(sun)
baseNode.addChildNode(mercuryRing)
baseNode.addChildNode(venusRing)
baseNode.addChildNode(earthRing)
baseNode.addChildNode(marsRing)
baseNode.addChildNode(jupiterRing)
baseNode.addChildNode(saturnRing)
baseNode.addChildNode(uranusRing)
baseNode.addChildNode(neptuneRing)
baseNode.position=SCNVector3.init(0,-0.5,-2)
//设置自转
rotateObject(rotation:-0.3, planet: sun, duration:1)
rotateObject(rotation:0.4, planet: mercury, duration:0.4)
rotateObject(rotation:0.25, planet: venus, duration:0.4)
rotateObject(rotation:0.3, planet: earth, duration:0.4)
rotateObject(rotation:0.8, planet: mars, duration:0.4)
rotateObject(rotation:1.0, planet: jupiter, duration:0.4)
rotateObject(rotation:1.25, planet: jupiter, duration:0.4)
rotateObject(rotation:0.25, planet: uranus, duration:0.4)
rotateObject(rotation:0.2, planet: neptune, duration:0.4)
rotateObject(rotation:0.6, planet: mercuryRing, duration:1)
rotateObject(rotation:0.4, planet: venusRing, duration:1)
rotateObject(rotation:0.25, planet: earthRing, duration:1)
rotateObject(rotation:0.2, planet: marsRing, duration:1)
rotateObject(rotation:0.45, planet: jupiterRing, duration:1)
rotateObject(rotation:0.34, planet: saturnRing, duration:1)
rotateObject(rotation:0.25, planet: uranusRing, duration:1)
rotateObject(rotation:0.2, planet: neptuneRing, duration:1)
sceneView.scene= scence
scence.rootNode.addChildNode(baseNode)
}