Saturday, November 21, 2026
HomeiOS Developmentswift - Unable to simplify 3 blocks of code to 1 in...

swift – Unable to simplify 3 blocks of code to 1 in iOS for augmented actuality

[ad_1]

I created a enjoyable little AR app that permits me to level my cellphone at 3 index playing cards, which have 2D drawings (xmas tree, ginger bread man, present), and have 3D variations of the photographs pop up.

Nevertheless, I’ve restricted my app to simply 3 pictures for now, as every image/3D combo has its personal block of code (proven beneath) however I wish to in some way consolidate to 1 block (even when I have to rename the picture information to a numbered format, however would respect any recommendation).

I’ve tried two strategies:

  1. one by which I’ve 3 blocks of code, one for every image (this methodology works – a translucent airplane seems simply over the index card and a 3d Object seems)
  2. one by which I’ve consolidated to 1 block of code (this methodology ends in solely the translucent airplane showing when pointing the digital camera on the index card)

3 blocks of code methodology

Screenshots of the app, together with the code are as follows:

Before
After

    // MARK: - ARSCNViewDelegate
    
    //the anchor would be the picture detected and the node might be a 3d object 
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        
        //the 3d object
        let node = SCNNode()
        
        //if this detects a airplane or level or something different then a picture, won't run this code
        if let imageAnchor = anchor as? ARImageAnchor {
            
            
            //this airplane is created from the picture detected (card).  need the width and top within the bodily world to be the identical as the cardboard, so successfully saying "take a look at the anchor picture discovered, and get its measurement properties”
            let airplane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, top: imageAnchor.referenceImage.physicalSize.top)
            
            //as soon as dimension is given to the airplane, use it to create a planenode which is a 3d object that might be rendered on high of the cardboard
            let planeNode = SCNNode(geometry: airplane)
            
            //makes the airplane translucent
            airplane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)
            
            //by default the airplane created is vertical so have to flip it to be flat with the cardboard detected
            planeNode.eulerAngles.x = -.pi / 2
            
            
            // faucet into the node created above and add a toddler node which would be the airplane node
            node.addChildNode(planeNode)
            
             // ------------3 BLOCKS OF CODE BELOW THAT NEED TO SIMPLIFY TO 1----------------
             // That is the primary block of code for the ChristmasTree.png picture/ChristmasTree.scn 3D object – word that the 2D picture isn't detected if “.png” is included on the finish of the picture anchor, nevertheless the “.scn” seems to be required for the 3D object).
            if imageAnchor.referenceImage.title == "ChristmasTree" {
                
                //create the 3d mannequin on high of the cardboard
                if let cardScene = SCNScene(named: "artwork.scnassets/ChristmasTree.scn") {
                    
                    //create a node that may characterize the 3d object.
                    if let cardNode = cardScene.rootNode.childNodes.first {
                        
                        //For the reason that 3d picture is rotated the opposite approach, have to deliver it ahead so identical as above solely with optimistic
                        cardNode.eulerAngles.x = .pi / 2
                        
                        planeNode.addChildNode(cardNode)
                    }
                }
            }
            
           // That is the second block of code for the Present.png picture/Present.scn 3D object
            if imageAnchor.referenceImage.title == "Present" {
                
                //create the 3d mannequin on high of the cardboard
                if let cardScene = SCNScene(named: "artwork.scnassets/Present.scn") {
                    
                    //create a node that may characterize the 3d object
                    if let cardNode = cardScene.rootNode.childNodes.first {
                        
                        //For the reason that 3d picture is rotated the opposite approach, have to deliver it ahead so identical as above solely with optimistic
                        cardNode.eulerAngles.x = .pi / 2
                        
                        planeNode.addChildNode(cardNode)
                    }
                }
            }
            
            // That is the third block of code for the GingerbreadMan.png picture/GingerbreadMan.scn 3D object
            if imageAnchor.referenceImage.title == "GingerbreadMan" {
                
                //create the 3d mannequin on high of the cardboard
                if let cardScene = SCNScene(named: "artwork.scnassets/GingerbreadMan.scn") {
                    
                    //create a node that may characterize the 3d object
                    if let cardNode = cardScene.rootNode.childNodes.first {
                        
                        //For the reason that 3d picture is rotated the opposite approach, have to deliver it ahead so identical as above solely with optimistic
                        cardNode.eulerAngles.x = .pi / 2
                        
                        planeNode.addChildNode(cardNode)
                    }
                }
            }
        }
        
        //this methodology is anticipating an output of SCNNode and it must ship that again onto the scene so it will probably render the 3d object
        return node
    }
    
}

One block of code methodology

I’ve revised the code as follows, nevertheless it has resulted in solely the translucent airplane showing when pointing the digital camera on the index card:

// MARK: - ARSCNViewDelegate
    
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        
        let node = SCNNode()
        
        if let imageAnchor = anchor as? ARImageAnchor {
            
            let airplane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, top: imageAnchor.referenceImage.physicalSize.top)
            
            let planeNode = SCNNode(geometry: airplane)
         
            airplane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)
            
            planeNode.eulerAngles.x = -.pi / 2
            
            node.addChildNode(planeNode)
            
            //------------single block of code------------

            let title = imageAnchor.referenceImage.title
            if ["ChristmasTree", "Gift", "GingerbreadMan"].incorporates(title) {
                    
                if let cardScene = SCNScene(named: "artwork.scnassets/(title).scn") {
                        
                    if let cardNode = cardScene.rootNode.childNodes.first {
                            
                        cardNode.eulerAngles.x = .pi / 2
                            
                        planeNode.addChildNode(cardNode)
                    }
                }
            }
            //------------single block of code------------
        }

        return node
    }
    
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments