I have 15 box and I add an image on each boxnode as first material. How can I load the images from Firebase and add an Image to each node. I already know how to load images from Firebase, I want to know the best way I can add an image on each 15 nodes, Thanks.
Here's the view controller:
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)
}
}
}
})
}
}
}
Here's the 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)")
}
}
}
}