gpt4 book ai didi

ios - 场景套件 SCNMorpher 不适用于 SCNSphere 等原语

转载 作者:行者123 更新时间:2023-12-01 17:38:54 26 4
gpt4 key购买 nike

有人让 SCNMorpher 工作吗?如果是这样,你到底做了什么?

我现在正在执行一个小型测试程序,它应该显示一个红色圆锥体,当我点击屏幕时,它应该(应该!)将其变形为一个球体/球。但唯一发生的事情是,第一个几何体消失了(变得非常小),而第二个几何体(Morphers 第一个目标)从未出现。

我必须错过一些非常简单的事情。有人可以帮我上马吗?

import SceneKit
private let DISTANCE_FAKTOR = CGFloat(sqrt(3.0)/2.0)

class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let (scene, view) = configScene(self.view.frame)
self.view = view

// Position camera to see picture full screen and light at same position
let pov = SCNVector3(x: 0.0, y: 0.0, z: Float(max(view.frame.width, view.frame.height) * DISTANCE_FAKTOR))
let pol = SCNVector3(x: 0.0, y: 0.0, z: Float(max(view.frame.width, view.frame.height) * DISTANCE_FAKTOR))

scene.rootNode.addChildNode(makeShapes()) // Create and add background plane
scene.rootNode.addChildNode(makeCamera(pov)) // Add camera to scene
scene.rootNode.addChildNode(makeLight(pol)) // Add light to scene

// add a tap gesture recognizer
view.gestureRecognizers = [UITapGestureRecognizer(target: self, action: "handleTap:")]
}

func handleTap(gestureRecognize: UIGestureRecognizer) {
NSLog("Tapped")
if let effectNode = (view as? SCNView)?.scene?.rootNode.childNodeWithName("EFFECT", recursively: true) {
NSLog("Animating")
animate(effectNode)
}
}

func makeShapes() -> SCNNode {
// First shape is a cone
let torus = SCNCone(topRadius: 0.0, bottomRadius: view.frame.width/2.0, height: view.frame.width/4.0)
torus.firstMaterial = SCNMaterial()
torus.firstMaterial?.diffuse.contents = UIColor.redColor()

// Now an additional sphere/ball
let sphere = SCNSphere(radius: view.frame.width/2.0)
sphere.firstMaterial = SCNMaterial()
sphere.firstMaterial?.diffuse.contents = UIColor.greenColor()

// Put all in the node
let node = SCNNode()
node.geometry = torus
node.morpher = SCNMorpher()
node.morpher?.targets = [sphere]
node.name = "EFFECT"

// I would expect now something between a ball and a torus in red/green
node.morpher?.setWeight(0.5, forTargetAtIndex: 0)
return node
}

func animate(node: SCNNode) {
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(5.0)
node.morpher?.setWeight(1.0, forTargetAtIndex: 0) // From torus to ball

SCNTransaction.setCompletionBlock {
NSLog("Transaction completing")
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(2.5)
node.morpher?.setWeight(0.0, forTargetAtIndex: 0) // And back
SCNTransaction.commit()
}
SCNTransaction.commit()
}

func configScene(frame: CGRect) -> (scene: SCNScene, view: SCNView) {
let view = SCNView()
view.frame = frame
view.autoresizingMask = UIViewAutoresizing.allZeros
view.backgroundColor = UIColor.blueColor()

let scene = SCNScene()
view.scene = scene

return (scene, view)
}

func makeCamera(pov: SCNVector3) -> SCNNode {
let camera = SCNCamera()
camera.zFar = Double(pov.z)

let node = SCNNode()
node.position = pov
node.camera = camera

return node
}

func makeLight(pol: SCNVector3) -> SCNNode {
let light = SCNLight()
light.type = SCNLightTypeOmni
light.zFar = CGFloat(pol.z)

let node = SCNNode()
node.position = pol
node.light = light

return node
}

override func shouldAutorotate() -> Bool {
return true
}

override func prefersStatusBarHidden() -> Bool {
return true
}

override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}

当我在代码中交换球和圆锥时,我可以看到球并在点击屏幕时消失,但圆锥从未出现。如果你想运行这段代码,只需在 Xcode 中创建一个新的 GameScene 项目并将这段代码复制到 GameViewController.swift

最佳答案

Has anybody got the SCNMorpher to work?

是的,我已经让 SCNMorpher 在自定义几何图形和从文件加载的几何图形之间工作。

您缺少的部分是所有变形目标需要具有相同数量的顶点,并且顶点需要具有相同的排列。这在 targets 属性的文档中进行了讨论:

The base geometry and all target geometries must be topologically identical — that is, they must contain the same number and structural arrangement of vertices.

您的示例(球体和圆锥体之间的早晨)不满足此要求,并且预计不会起作用。

您可以尝试在两个不同的球体(具有相同数量的段!)之间变形,看看它确实工作。

关于ios - 场景套件 SCNMorpher 不适用于 SCNSphere 等原语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29252017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com