本文价值与收获
看完本文后,您将能够作出下面的界面
截屏2020-07-29 上午9.15.24.png
地球仪
网格
SceneKit 绘制Sphere
基础知识
SCNSphere
球体(或球或地球仪)几何。
class SCNSphere : SCNGeometry
使用教程
球体及其性质
SCNSphere
球体定义了一个曲面,该曲面的每个点均与其中心等距,该曲面位于其局部坐标空间的原点。您可以使用其radius属性在所有三个维度中定义球体的大小。
SceneKit使用多边形网格逼近球体的曲面。构造网格有两种选择:
默认情况下,SceneKit使用矩形网格构造球体,就像地球上的纬度和经度线一样。球体的每个极点都有一个顶点,其属性决定了从一个极点到另一个极点的沿其表面的分割数(或纬度线)以及在一个水平面内沿其圆周的分割数(或经度线) )。
如果将球体的属性设置为,SceneKit将通过依次细分规则二十面体的三角形曲面来构造球体。对于测地球,该属性对数缩放以确定细分的数量,大致近似于由相同线段数的非测地球生成的顶点的数量。isGeodesictrueSCNSphere
无论采用哪种近似,增加SCNSphere属性都会产生更多的顶点和更平滑的曲面,从而可以提高渲染质量,但会降低渲染性能。
实战代码
```
import SwiftUI
import SceneKit
let scene1 = SCNScene()
let scene2 = SCNScene()
let scene3 = SCNScene()
let scene4 = SCNScene()
struct ContentView: View {
init(){
drawGrid2(scene:scene3,c3:SCNVector3(x: 10, y: 10, z: 10))
drawGrid(scene:scene1)
drawLineOfSpheresTouching(scene:scene2)
// draw
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 5.0)
scene4.rootNode.addChildNode(cameraNode)
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 5)
scene4.rootNode.addChildNode(lightNode)
let stars = SCNParticleSystem(named: "StarsParticles.scnp", inDirectory: nil)!
scene4.rootNode.addParticleSystem(stars)
planetNodeName = "Earth"
let presentedNode = PresentedPlanetNode()
scene4.rootNode.addChildNode(presentedNode)
}
var body: some View {
NavigationView{
List{
NavigationLink(
destination: SceneView(scene:scene1,options: [.allowsCameraControl,.autoenablesDefaultLighting]
),
label: {
Text("网格排列")
})
NavigationLink(
destination: SceneView(scene:scene2,options: [.allowsCameraControl,.autoenablesDefaultLighting]
),
label: {
Text("线性排列")
})
NavigationLink(
destination: SceneView(scene:scene3,options: [.allowsCameraControl,.autoenablesDefaultLighting]
),
label: {
Text("绘制点个Sphere")
})
NavigationLink(
destination: SceneView(scene:scene4,options: [.allowsCameraControl]
),
label: {
Text("地球仪")