【问题标题】:How to use a SKScene as texture for SCNSphere?如何使用 SKScene 作为 SCNSphere 的纹理?
【发布时间】:2020-04-01 06:18:29
【问题描述】:

我想创建一个绿色球体,其表面上有一个红点。我发现最有效的方法之一是使用 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)

  }
}

请帮我解决这个问题。

【问题讨论】:

    标签: sprite-kit scenekit swiftui swift5 sktexture


    【解决方案1】:

    diMove 从未被调用过。

    如果你真的调用它,球体就会变成绿色......

     let material = Material()
    
        sphere.firstMaterial?.diffuse.contents = material
    
        material.didMove(to: SKView())
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2018-05-03
      • 2015-05-29
      • 2019-07-31
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多