singularity
singularity

Reputation: 109

Can I copy a SCNNode and keep the scale

I would like to copy a SCNNode multiple times, have different materials for each node and different positions. However keeping the same scale. So, if I change the scale for the node I copy, all copied nodes should change.

In the code below, when I run changeScale(), the copied node scale does not change.

Is there a way I can change the scale of all copied Nodes or size of geometry together. Without enumerating or changing them individually

let mainNode = SCNNode()
let mainGeo = SCNPlane(width: CGFloat(4), height: CGFloat(4))
mainNode.geometry = mainGeo

for var i = 1; i <= 10; i += 1 {
    let thisNode = mainNode.copy() as! SCNNode
    thisNode.position = SCNVector3Make( Float(rx), Float(ry), Float(rz) )

    thisNode.geometry = thisNode.geometry!.copy() as? SCNGeometry
    thisNode.geometry?.firstMaterial = thisNode.geometry?.firstMaterial!.copy() as? SCNMaterial

    if i == 0 {
        thisNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blueColor()
    } else {
        thisNode.geometry?.firstMaterial?.diffuse.contents = UIColor.redColor()
    }
    scene.rootNode.addChildNode(thisNode)

}

func changeScale() {
    mainNode.scale = SCNVector3Make(7, 7, 7)
}

Upvotes: 0

Views: 930

Answers (2)

Laurent Crivello
Laurent Crivello

Reputation: 3931

You may try to use clone instead of copy:

let thisNode = mainNode.clone() as! SCNNode

Upvotes: 0

singularity
singularity

Reputation: 109

I am not sure whether this is the right answer or not. As I am not an expert in Swift or ios. It seems like when a node is copied or cloned, the node carries no properties apart from the assigned information on geometries etc. I had initially thought some properties could be kept the same for all copied nodes, like scale or position.

What I wanted was to have different positions and materials for every node at creation, with a changeable scale of all the nodes or size of geometries together.

Now, as the nodes and geometry's are created multiple times, they are all different and cannot be sized or scaled together

So what I did:

  • Copied the main node multiple times, gave it different positions
  • Created one main geometry (outside), set it to the main node with a
    particular size. So all the main nodes use this geometry
  • Added a subnode to the main node, with different material properties
  • Added a SCNTransformConstraint to the subnode, to transform according to the size of the main material

So now whenever I edit the geometry size the subnode size changes all together. I am not sure how this method is with speed/performance. But it seems better than enumerating through every node

Upvotes: 0

Related Questions