swift - 将 SCNNode 排列成圆形
问题描述
我正在自动创建多个节点,并且我想将它们安排在我周围,因为目前我只是将当前 X 位置增加了 0.1。
capsuleNode.geometry?.firstMaterial?.diffuse.contents = imageView
capsuleNode.position = SCNVector3(self.counterX, self.counterY, self.counterZ)
capsuleNode.name = topic.name
self.sceneLocationView.scene.rootNode.addChildNode(capsuleNode)
self.counterX += 0.1
所以问题是,我怎样才能让所有这些都在我身边,而不是仅仅在一条线上?
你们中有人对此有一些数学功能吗?谢谢!
解决方案
使用此代码(macOS 版本)对其进行测试:
import SceneKit
class GameViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.allowsCameraControl = true
scnView.backgroundColor = NSColor.black
for i in 1...12 { // HERE ARE 12 SPHERES
let sphereNode = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode.position = SCNVector3(0, 0, 0)
// ROTATE ABOUT THIS OFFSET PIVOT POINT
sphereNode.simdPivot.columns.3.x = 5
sphereNode.geometry?.firstMaterial?.diffuse.contents = NSColor(calibratedHue: CGFloat(i)/12,
saturation: 1,
brightness: 1,
alpha: 1)
// ROTATE ABOUT Y AXIS (STEP is 30 DEGREES EXPRESSED IN RADIANS)
sphereNode.rotation = SCNVector4(0, 1, 0, (-CGFloat.pi * CGFloat(i))/6)
scene.rootNode.addChildNode(sphereNode)
}
}
}
PS这是创建90个球体的代码:
for i in 1...90 {
let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.1))
sphereNode.position = SCNVector3(0, 0, 0)
sphereNode.simdPivot.columns.3.x = 5
sphereNode.geometry?.firstMaterial?.diffuse.contents = NSColor(calibratedHue: CGFloat(i)/90, saturation: 1, brightness: 1, alpha: 1)
sphereNode.rotation = SCNVector4(0, 1, 0, (-CGFloat.pi * (CGFloat(i))/6)/7.5)
scene.rootNode.addChildNode(sphereNode)
}
推荐阅读
- android - Flutter 移动应用程序中的 Flurry 崩溃报告
- flutter - 试图停止计时器但计时器没有停止
- php - 运行 PHPUnit 测试时无法加载 Xdebug
- android - 当我使用 viewmodel 时,为什么 onClick 方法不调用 ratingBar?
- python - 将数组字符串值添加到 pandas.DataFrame 时,它们会转换为浮点数
- telegram - 如何使用 python-telegram-bot (Bot API 5.1) 跟踪用户何时踢出 Telegram Bot
- maven - 就在创建新的 Maven 项目时,我收到此错误“无法计算构建计划插件 org.apache.maven.plugins maven-resources-plugin:2.6”
- javascript - 数据表中只显示一个数据
- python - 在python中重命名具有相似名称的多列
- javascript - 如果在定义的数组中找不到 JSON 属性,则删除