SceneKit 自定义几何形状未显示
SceneKit Custom Geometry Shape not showing up
我是 Swift 和 SceneKit 的新手,我目前的问题是我尝试创建的自定义形状没有显示,即使框架中的原始形状显示正常。
我一直在学习 https://www.raywenderlich.com/1261-scene-kit-tutorial-with-swift-part-1-getting-started 的教程。
我也查看了 SO 上的答案:。我在这里查看了其他答案,但 none 对我有用。
这是我的代码:
import UIKit
import SceneKit
import QuartzCore
class GameViewController: UIViewController {
var scnView: SCNView!
var scnScene: SCNScene!
var cameraNode: SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
setupScene()
setupCamera()
let lightNode0 = SCNNode()
lightNode0.light = SCNLight()
lightNode0.light!.type = .omni
lightNode0.position = SCNVector3(x: 0, y: 10, z: 10)
scnScene.rootNode.addChildNode(lightNode0)
let lightNode1 = SCNNode()
lightNode1.light = SCNLight()
lightNode1.light!.type = .omni
lightNode1.position = SCNVector3(5, -10, 0)
scnScene.rootNode.addChildNode(lightNode1)
spawnShape()
}
func shouldAutorotate() -> Bool {
return true
}
func prefersStatusBarHidden() -> Bool {
return true
}
func setupView() {
scnView = self.view as! SCNView
// 1
scnView.showsStatistics = true
// 2
scnView.allowsCameraControl = true
// 3
scnView.autoenablesDefaultLighting = true
}
func setupScene() {
scnScene = SCNScene()
scnView.scene = scnScene
}
func setupCamera() {
// 1
cameraNode = SCNNode()
// 2
cameraNode.camera = SCNCamera()
// 3
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
// 4
scnScene.rootNode.addChildNode(cameraNode)
}
这里是生成自定义形状的函数:
func spawnShape() {
// 1
var geometry:SCNGeometry
let positions = [
SCNVector3(-2, 1.5, 0), //0
SCNVector3(-2, 1.5, 0), //1
SCNVector3(2, -1.5, 0), //2
SCNVector3(2, 1.5, 0), //3
SCNVector3(-2, 1.5, 0.4), //4
SCNVector3(2, 1.5, 0.4) //5
]
let source = SCNGeometrySource(vertices: positions)
let indices:[CInt] = [
0, 2, 1,
0, 3, 2,
0, 4, 5,
0, 5 ,3,
4, 1, 2,
4, 2, 5
]
let element = SCNGeometryElement(indices: indices, primitiveType:.triangles)
// 4
geometry = SCNGeometry(sources: [source], elements: [element])
let geometryNode = SCNNode(geometry: geometry)
// 5
scnScene.rootNode.addChildNode(geometryNode)
}
}
试试我的轻量级代码(用于快速测试的 macOS 版本)。
正在运行:
import SceneKit
class GameViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
let geometry: SCNGeometry?
let positions = [
SCNVector3(0, 1, 0),
SCNVector3(-0.5, 0, 0.5),
SCNVector3(0.5, 0, 0.5),
SCNVector3(0.5, 0, -0.5),
SCNVector3(-0.5, 0, -0.5),
SCNVector3(0, -1, 0),
]
let source = SCNGeometrySource(vertices: positions)
let indices: [UInt32] = [
0, 1, 2,
2, 3, 0,
3, 4, 0,
4, 1, 0,
1, 5, 2,
2, 5, 3,
3, 5, 4,
4, 5, 1
]
let element = SCNGeometryElement(indices: indices, primitiveType:.triangles)
geometry = SCNGeometry(sources: [source], elements: [element])
geometry!.firstMaterial?.diffuse.contents = NSColor.red
let geometryNode = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(geometryNode)
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.allowsCameraControl = true
scnView.autoenablesDefaultLighting = true
scnView.backgroundColor = NSColor.black
}
}
我是 Swift 和 SceneKit 的新手,我目前的问题是我尝试创建的自定义形状没有显示,即使框架中的原始形状显示正常。
我一直在学习 https://www.raywenderlich.com/1261-scene-kit-tutorial-with-swift-part-1-getting-started 的教程。
我也查看了 SO 上的答案:
这是我的代码:
import UIKit
import SceneKit
import QuartzCore
class GameViewController: UIViewController {
var scnView: SCNView!
var scnScene: SCNScene!
var cameraNode: SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
setupScene()
setupCamera()
let lightNode0 = SCNNode()
lightNode0.light = SCNLight()
lightNode0.light!.type = .omni
lightNode0.position = SCNVector3(x: 0, y: 10, z: 10)
scnScene.rootNode.addChildNode(lightNode0)
let lightNode1 = SCNNode()
lightNode1.light = SCNLight()
lightNode1.light!.type = .omni
lightNode1.position = SCNVector3(5, -10, 0)
scnScene.rootNode.addChildNode(lightNode1)
spawnShape()
}
func shouldAutorotate() -> Bool {
return true
}
func prefersStatusBarHidden() -> Bool {
return true
}
func setupView() {
scnView = self.view as! SCNView
// 1
scnView.showsStatistics = true
// 2
scnView.allowsCameraControl = true
// 3
scnView.autoenablesDefaultLighting = true
}
func setupScene() {
scnScene = SCNScene()
scnView.scene = scnScene
}
func setupCamera() {
// 1
cameraNode = SCNNode()
// 2
cameraNode.camera = SCNCamera()
// 3
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
// 4
scnScene.rootNode.addChildNode(cameraNode)
}
这里是生成自定义形状的函数:
func spawnShape() {
// 1
var geometry:SCNGeometry
let positions = [
SCNVector3(-2, 1.5, 0), //0
SCNVector3(-2, 1.5, 0), //1
SCNVector3(2, -1.5, 0), //2
SCNVector3(2, 1.5, 0), //3
SCNVector3(-2, 1.5, 0.4), //4
SCNVector3(2, 1.5, 0.4) //5
]
let source = SCNGeometrySource(vertices: positions)
let indices:[CInt] = [
0, 2, 1,
0, 3, 2,
0, 4, 5,
0, 5 ,3,
4, 1, 2,
4, 2, 5
]
let element = SCNGeometryElement(indices: indices, primitiveType:.triangles)
// 4
geometry = SCNGeometry(sources: [source], elements: [element])
let geometryNode = SCNNode(geometry: geometry)
// 5
scnScene.rootNode.addChildNode(geometryNode)
}
}
试试我的轻量级代码(用于快速测试的 macOS 版本)。
正在运行:
import SceneKit
class GameViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
let geometry: SCNGeometry?
let positions = [
SCNVector3(0, 1, 0),
SCNVector3(-0.5, 0, 0.5),
SCNVector3(0.5, 0, 0.5),
SCNVector3(0.5, 0, -0.5),
SCNVector3(-0.5, 0, -0.5),
SCNVector3(0, -1, 0),
]
let source = SCNGeometrySource(vertices: positions)
let indices: [UInt32] = [
0, 1, 2,
2, 3, 0,
3, 4, 0,
4, 1, 0,
1, 5, 2,
2, 5, 3,
3, 5, 4,
4, 5, 1
]
let element = SCNGeometryElement(indices: indices, primitiveType:.triangles)
geometry = SCNGeometry(sources: [source], elements: [element])
geometry!.firstMaterial?.diffuse.contents = NSColor.red
let geometryNode = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(geometryNode)
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.allowsCameraControl = true
scnView.autoenablesDefaultLighting = true
scnView.backgroundColor = NSColor.black
}
}