【发布时间】:2020-04-02 16:51:38
【问题描述】:
我正在尝试制作一个游戏,其中给你一个球体,它的半径和球体表面上的一个点。我使用 SceneKit 作为我的基本框架。球体是SCNSphere,半径是SCNCylinder,它被小心地放置,使它看起来像球体的半径(圆柱体的高度等于球体的半径,它位于球体)。球体表面上的点是应用在球体表面上的SKScene作为其材质。
我的目标是使圆柱体的一端固定在球体的中心并旋转它,使其尖端可以在球体表面的每个点上。
我试图通过使用SCNAction 来达到同样的效果,但它与我想要的效果相去甚远。
我的代码是:
struct SceneKitView: UIViewRepresentable {
func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
//SCNScene
let sceneView = SCNView()
sceneView.scene = SCNScene()
sceneView.allowsCameraControl = true
sceneView.autoenablesDefaultLighting = true
sceneView.backgroundColor = UIColor.white
sceneView.frame = CGRect(x: 0, y: 10, width: 0, height: 1)
//A SKScene used as a material for a Sphere
let surfacematerial = SKScene(size: CGSize(width: 300, height: 200))
let point = SKShapeNode(circleOfRadius: 2)
surfacematerial.backgroundColor = SKColor.blue
point.fillColor = SKColor.red
point.lineWidth = 0
point.position = CGPoint(x: surfacematerial.size.width/2.0, y: surfacematerial.size.height/2.0)
surfacematerial.addChild(point)
//The SCNSphere
let sphere = SCNSphere(radius: CGFloat(2))
let spherematerial = SCNMaterial()
spherematerial.diffuse.contents = surfacematerial
sphere.materials = [spherematerial]
let spherenode = SCNNode(geometry: sphere)
spherenode.position = SCNVector3(x: 1.0, y: 1.0, z: 1.0)
spherenode.opacity = CGFloat(0.6)
//The radius of the sphere( a SCNCylinder )
let cylinder = SCNCylinder(radius: 0.02, height: 2.0)
let cylindernode = SCNNode(geometry: cone)
cylindernode.position = SCNVector3(x: 1, y: 2, z: 1)
cylinder.firstMaterial?.diffuse.contents = UIColor.green
//Rotating the radius
func degreesToRadians(_ degrees: Float) -> CGFloat {
return CGFloat(degrees * .pi / 180)
}
let rotate = SCNAction.rotate(by: degreesToRadians(90.0), around: SCNVector3(x: 1, y: 0 , z: 0), duration: 10)
sceneView.scene?.rootNode.addChildNode(conenode)
sceneView.scene?.rootNode.addChildNode(spherenode)
return sceneView
}
func updateUIView(_ uiView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {
}
typealias UIViewType = SCNView
}
请帮我解决这个问题。
【问题讨论】: