android 地球仪源码,SwiftUI SceneKit 原生绘制Sphere球体与地球仪(教程含源码)

本文介绍了如何在SceneKit中使用SCNSphere类创建地球仪网格,包括默认的纬度和经度线网格以及通过细分二十面体的方法。详细讲解了球体的属性和渲染质量与性能之间的平衡,并提供了实战代码示例,展示了不同类型的球体绘制效果。
摘要由CSDN通过智能技术生成

本文价值与收获

看完本文后,您将能够作出下面的界面

85f9c65146b889ad46779cb82c5ddede.png

截屏2020-07-29 上午9.15.24.png

d5f67259e6201c79a8d6813f69ce2927.png

地球仪

0bb196f28e8cdd32143fe980eb342cd1.png

网格

42a9079309fa772f7763090a83d6b5a8.png

SceneKit 绘制Sphere

基础知识

SCNSphere

球体(或球或地球仪)几何。

class SCNSphere : SCNGeometry

使用教程

球体及其性质

5a1cd4c5824940ca5f3cfd76e040be9d.png

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("地球仪")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值