mercredi 17 août 2016

SCNNode: Random color for each individial node, not all? - Swift, SceneKit

If I have a function that spawns a cube right next to a previous cube whenever the user taps the screen and changes the color of the cubes as well, how can I make sure that each cube can become a random color and not the all the cubes become the same random color? I don't think I can use the same geometry for each cube because then it would inherit whatever material I originally gave to it. I want each cube that is spawned to select its own random color, not all the cubse select the same random color that it picks when tapping. I have a random color function, and it works. But if I spawn 5 cubes, and tap the screen, all 5 cubes become the same random color. But instead, I want each individual cube to be its own random color. I am using Swift, and SceneKit. Thanks for your help in advance!

  func randomColor2() -> UIColor{
    let red = CGFloat(drand48())
    let green = CGFloat(drand48())
    let blue = CGFloat(drand48())
    return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

Right now I just say, inside of my touches began:

boxNodeMaterial.diffuse.contents = randomColor2

And I also call my createBox function which adds another box node beside the original box. I want this second box to pick it's own random color, not the same one as the original. I want this to continue for each box that is added. Box 1 can be red, green, or blue. Box 2 can be red, green, or blue, Box 3 and so forth. NOT Box 1, Box 2, Box 3 pick red for example and makes them all the same color.




Aucun commentaire:

Enregistrer un commentaire