2016-08-24 77 views
0

目標是縮放SCNNode並將其垂直居中放置在其父節點中。SceneKit:垂直中心縮放的SCNNode在父節點內嗎?

但是,縮放SCNNode不會影響其邊界框,並且計算縮放的高度不起作用。如果沒有一個精確的高度,你如何能夠將節點垂直居中放置在父節點中?

要說明使用縮放高度的問題,請參閱附加文件Tiki.dae。資產的原始高度(如邊界框所示)爲324.36。但是,如果將Y標尺設置爲0.01,則高度不會變爲〜3.24。它變得小於3,你可以通過將它舒適地安裝在高度爲3(半徑爲1.5)的球體內來證明它。

下面的代碼嘗試將縮放的節點居中在其父代中,但它不起作用。

注意:參考節點是WWDC 2015狐狸演示中的狐狸/熊貓參考節點。

  // Create reference node 
      let referenceNode = SCNReferenceNode(URL: referenceURL) 
      referenceNode?.load() 

      // Scale reference node 
      let scale = Float(3) 
      referenceNode?.scale = SCNVector3(x: scale, y: scale, z: scale) 

      // Create geometry for sphere 
      let sphereGeometry = SCNSphere(radius: (gridSphere.geometry as! SCNSphere).radius) 
      //sphereGeometry.materials = gridSphere.geometry!.materials 
      sphereGeometry.firstMaterial!.diffuse.contents = gPurpleColor 

      // Create sphere to hold reference node, position at same place as <gridSphere> 
      let liveSphere = SCNNode(geometry: sphereGeometry) 
      liveSphere.position = gridSphere.position 

      // Center reference node inside <liveSphere> 
      var min = SCNVector3Zero 
      var max = SCNVector3Zero 
      referenceNode?.getBoundingBoxMin(&min, max: &max) 
      let referenceNodeHeight = max.y - min.y 
      referenceNode?.position = SCNVector3(x: 0, y: 0 - referenceNodeHeight, z: 0) 

      // Add reference node to <liveSphere> 
      liveSphere.addChildNode(referenceNode!) 

      // This value never changes no matter the scale value??? 
      print(referenceNodeHeight) 
+0

該示例看起來不起作用。 'referenceNode'沒有孩子,永遠不會被添加到場景中。由於它沒有孩子,也沒有幾何體,所以0的高度似乎是合理的。 'liveSphere'永遠不會被添加到場景中。 'gridSphere'顯然是在其他地方定義的?請注意,根據https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNReferenceNode_Class/「SCNReferenceNode」,其目的是作爲從場景文件加載的其他內容的佔位符。 –

+0

@HalMueller oops刪除了太多代碼以簡化它。重新添加正確的代碼。 – Crashalot

+0

@HalMueller參考節點是WWDC 2015狐狸演示中的狐狸/熊貓,爲簡單起見,此處省略。這些文件是否也會上傳? – Crashalot

回答

0

這裏是一個遊樂場,它將一個立方體(代替您的參考節點)添加爲球體的一個孩子。該立方體響應縮放(請參閱「//縮放參考節點」後面的行)。

//: Playground - noun: a place where people can play 

import Cocoa 
import SceneKit 
import XCPlayground 

public class GizmoNode: SCNNode { 

    required public override init() { 
     super.init() 
     let axisLength = CGFloat(3.0) 
     let offset = CGFloat(axisLength/2.0) 
     let axisSide = CGFloat(0.2) 
     let chamferRadius = CGFloat(axisSide) 

     let xBox = SCNBox(width: axisLength, height: axisSide, length: axisSide, chamferRadius: chamferRadius) 
     xBox.firstMaterial?.diffuse.contents = NSColor.redColor() 
     let yBox = SCNBox(width: axisSide, height: axisLength, length: axisSide, chamferRadius: chamferRadius) 
     yBox.firstMaterial?.diffuse.contents = NSColor.greenColor() 
     let zBox = SCNBox(width: axisSide, height: axisSide, length: axisLength, chamferRadius: chamferRadius) 
     zBox.firstMaterial?.diffuse.contents = NSColor.blueColor() 
     let xNode = SCNNode(geometry: xBox) 
     let yNode = SCNNode(geometry: yBox) 
     let zNode = SCNNode(geometry: zBox) 
     self.addChildNode(xNode) 
     self.addChildNode(yNode) 
     self.addChildNode(zNode) 
     print (xNode.position) 
     print (yNode.position) 
     print (zNode.position) 
     xNode.position.x = offset 
     yNode.position.y = offset 
     zNode.position.z = offset 
     print (xNode.pivot) 
     print (yNode.pivot) 
     print (zNode.pivot) 
    } 

    required public init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) 
let scene = SCNScene() 

sceneView.scene = scene 
sceneView.backgroundColor = NSColor.blackColor() 
sceneView.allowsCameraControl = true 

let gizmo = GizmoNode() 
scene.rootNode.addChildNode(gizmo) 

XCPlaygroundPage.currentPage.liveView = sceneView 

// Create reference node 
let cubeSize = CGFloat(0.5) 
let cubeGeometry = SCNBox(width: cubeSize, height: cubeSize, length: cubeSize, chamferRadius: 0.0) 
let referenceNodeStandIn = SCNNode(geometry: cubeGeometry) 
//referenceNodeStandIn?.load() 
let cubeColor = NSColor.whiteColor().colorWithAlphaComponent(0.5) 
cubeGeometry.firstMaterial!.diffuse.contents = cubeColor 

// Scale reference node 
let scale = CGFloat(8) 
referenceNodeStandIn.scale = SCNVector3(x: scale, y: scale, z: scale) 

// Create geometry for sphere 
let sphereRadius = CGFloat(2.0) 
let sphereGeometry = SCNSphere(radius: sphereRadius) 
//sphereGeometry.materials = gridSphere.geometry!.materials 
let gPurpleColor = NSColor.purpleColor().colorWithAlphaComponent(1.0) 
sphereGeometry.firstMaterial!.diffuse.contents = gPurpleColor 

// Create sphere to hold reference node, position at same place as <gridSphere> 
let liveSphere = SCNNode(geometry: sphereGeometry) 
//liveSphere.position = gridSphere.position 
scene.rootNode.addChildNode(liveSphere) 

// Center reference node inside <liveSphere> 
var min = SCNVector3Zero 
var max = SCNVector3Zero 
referenceNodeStandIn.getBoundingBoxMin(&min, max: &max) 
print("min: \(min) max: \(max)") 
let referenceNodeHeight = max.y - min.y 
//referenceNodeStandIn.position = SCNVector3(x: 0, y: 0 - referenceNodeHeight, z: 0) 

// Add reference node to <liveSphere> 
liveSphere.addChildNode(referenceNodeStandIn) 

// This value never changes no matter the scale value because it's in local coordinate space 
print("reference node height", referenceNodeHeight) 
+0

謝謝哈!你太客氣了。但是究竟有什麼區別,或者更重要的是,在父節點中放大縮放節點的教訓呢?好像你註釋掉了這個位置,那麼你如何居中節點? – Crashalot