SCNNode和Firebase

时间:2017-01-01 00:19:24

标签: swift firebase scenekit firebase-storage scnnode

我有15个盒子,我在每个盒子节点上添加一个图像作为第一个素材。如何从Firebase加载图像并将图像添加到每个节点。我已经知道如何从Firebase加载图像,我想知道在每个15个节点上添加图像的最佳方式,谢谢。

这是视图控制器:

    class newsVC: UIViewController, presentVCProtocol {

    @IBOutlet var scnView: SCNView!

    var newsScene = NewsScene(create: true)

    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!

    var posts = [ArtModel]()
    var post: ArtModel!
    var arts = [ArtModel]()

    static var imageCache: NSCache<NSString, UIImage> = NSCache()

    var type: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        let scnView = self.scnView!
        let scene = newsScene
        scnView.scene = scene
        scnView.autoenablesDefaultLighting = true
        scnView.backgroundColor = UIColor.white

DataService.ds.REF_USERS.child((FIRAuth.auth()?.currentUser?.uid)!).child("arts").observe(.value) { (snapshot: FIRDataSnapshot) in
            self.posts = []
            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for snap in snapshot {
                    if let postDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key
                        let post = ArtModel(key: key, artData: postDict)
                        self.posts.insert(post, at: 0)
                    }
                }
            }
        }
    }

    func configureView(_ post: ArtModel, img: UIImage? = nil, imageView: FXImageView? = nil) {
        self.post = post
        if img != nil {
            self.newsScene.setup(image: img!)
            print("IMAGE:\(img)")
        } else {
            let ref = FIRStorage.storage().reference(forURL: post.imgUrl)
            ref.data(withMaxSize:  2 * 1024 * 1024, completion: { (data, error) in
                if error != nil {
                    print("JESS: Unable to download image from Firebase storage")
                    print("Unable to download image: \(error?.localizedDescription)")
                } else {
                    print("JESS: Image downloaded from Firebase storage")
                    if let imgData = data {
                        if let img = UIImage(data: imgData) {
                            self.newsScene.setup(image: img)
                            print("IMAGE:\(img)")
                            ProfileVC.imageCache.setObject(img, forKey: post.imgUrl as NSString)
                        }
                    }
                }
            })
        }
    }
}

这是NewsScene:

import SceneKit
import CoreMotion
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase

class NewsScene: SCNScene {

    var geometry = SCNBox()
    var boxnode = SCNNode()
    var art: ArtModel!
    var artImage = UIImage()
    var index = IndexPath()
    var geo = SCNBox()
    var cameranode = SCNNode()


    convenience init(create: Bool) {
        self.init()

        setup(image: artImage)
    }

    func setup(image: UIImage) {

        self.artImage = image
        typealias BoxDims = (width: CGFloat, height: CGFloat,
            length: CGFloat, chamferRadius: CGFloat)

        let box1Dim = BoxDims(CGFloat(0.8), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box2Dim = BoxDims(CGFloat(0.7), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box3Dim = BoxDims(CGFloat(0.8), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box4Dim = BoxDims(CGFloat(0.8), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))
        let box5Dim = BoxDims(CGFloat(0.9), CGFloat(1.0), CGFloat(0.10), CGFloat(0.01))
        let box6Dim = BoxDims(CGFloat(0.4), CGFloat(0.5), CGFloat(0.10), CGFloat(0.01))
        let box7Dim = BoxDims(CGFloat(0.9), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box8Dim = BoxDims(CGFloat(0.7), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box9Dim = BoxDims(CGFloat(0.9), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))
        let box10Dim = BoxDims(CGFloat(0.6), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box11Dim = BoxDims(CGFloat(0.7), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box12Dim = BoxDims(CGFloat(0.8), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box13Dim = BoxDims(CGFloat(0.6), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box14Dim = BoxDims(CGFloat(0.6), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box15Dim = BoxDims(CGFloat(0.9), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))

        let allBoxDims = [box1Dim, box2Dim, box3Dim, box4Dim, box5Dim, box6Dim, box7Dim, box8Dim,box9Dim,box10Dim,box11Dim,box12Dim,box13Dim,box14Dim,box15Dim ]

        let offset: Int = 50
        var boxCounter: Int = 0
        for xIndex: Int in 0...2 {
            for yIndex: Int in 0...4 {
                // create a geometry
                let boxDim = allBoxDims[boxCounter]
                geo = SCNBox(width: boxDim.width, height: boxDim.height, length: boxDim.length, chamferRadius: boxDim.chamferRadius)


                let material = SCNMaterial()
                material.diffuse.contents = image
                geo.firstMaterial = material
                boxCounter = boxCounter + 1


                boxnode = SCNNode(geometry: geo)
                boxnode.position.x = Float(xIndex - offset)
                boxnode.position.y = Float(yIndex - offset)
                self.rootNode.addChildNode(boxnode)

            }
        }
    }

    func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {
        if let motion = motion {
            boxnode.orientation = motion.gaze(atOrientation: UIApplication.shared.statusBarOrientation)
            if error != nil {
                print("DEVICEDIDMOVE: \(error?.localizedDescription)")
            }
        }
    }
}

0 个答案:

没有答案