我想创建一个绿色的球体,在它的表面上有一个红点。我发现的最有效的方法之一是使用SKScene作为具有所需属性的球体的纹理,并在SwiftUI中将其作为一个整体显示在框架内。运行代码后,我得到一个纯白色的球体。
我的代码是:
import SwiftUI
import SceneKit
import SpriteKit
import UIKit
//SKScene containing the point
class material: SKScene{
let point = SKShapeNode(circleOfRadius: 0.5)
override func didMove(to view: SKView) {
backgroundColor = SKColor.green
point.fillColor = SKColor.red
point.position = CGPoint(x: 100, y: 100)
physicsWorld.gravity = CGVector.zero
addChild(point)
}
}
//SCNScene containing the sphere
struct SceneKitView: UIViewRepresentable {
func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
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)
let sphere = SCNSphere(radius: CGFloat(2))
sphere.firstMaterial?.diffuse.contents = material()
let spherenode = SCNNode(geometry: sphere)
spherenode.position = SCNVector3(x: 10.0, y: 10.0, z: 10.0)
sceneView.scene?.rootNode.addChildNode(spherenode)
return sceneView
}
func updateUIView(_ uiView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {
}
typealias UIViewType = SCNView
}
//SwiftUI code
struct ContentView: View {
var body: some View {
SceneKitView()
.frame(width: 300, height: 300)
}
}请帮我解决这个问题。
发布于 2019-12-08 02:02:57
diMove从未被调用过。
如果你真的这么说,球体会变绿……
let material = Material()
sphere.firstMaterial?.diffuse.contents = material
material.didMove(to: SKView())https://stackoverflow.com/questions/59226961
复制相似问题