首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SCNSphere中使用SKScene作为纹理?

如何在SCNSphere中使用SKScene作为纹理?
EN

Stack Overflow用户
提问于 2019-12-07 22:28:20
回答 1查看 111关注 0票数 0

我想创建一个绿色的球体,在它的表面上有一个红点。我发现的最有效的方法之一是使用SKScene作为具有所需属性的球体的纹理,并在SwiftUI中将其作为一个整体显示在框架内。运行代码后,我得到一个纯白色的球体。

我的代码是:

代码语言:javascript
复制
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)

  }
}

请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2019-12-08 02:02:57

diMove从未被调用过。

如果你真的这么说,球体会变绿……

代码语言:javascript
复制
 let material = Material()

    sphere.firstMaterial?.diffuse.contents = material

    material.didMove(to: SKView())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59226961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档