I'm trying to draw an Earth, as I've found the textures in this site: http://planetpixelemporium.com/earth.html

I loaded the diffuse, specular and bump maps:

let earthGeometry = SCNSphere(radius: radius)

let earthMap = UIImage(named: "Earth_map")
let earthBump = UIImage(named: "Earth_bump")
let earthSpec = UIImage(named: "Earth_specular")

earthGeometry.firstMaterial!.diffuse.contents = earthMap
earthGeometry.firstMaterial!.specular.contents = earthSpec

let earth = SCNNode(geometry: earthGeometry)

But there is no property for the bump map. The only way that comes to my mind is to use a MDLMaterial but I see no way to convert the material back to a SCNMaterial. The method:

+ (instancetype)materialWithMDLMaterial:(MDLMaterial *)mdlMaterial

Runs just in Objective-C and not in Swift. Does anyone know how to convert it to a SCNMaterial, or alternatively to use another way to apply a bump map in a SCNMaterial?

There isn’t a built-in way to apply a grayscale bump map like the one you have; what SceneKit expects is a normal map. It’s pretty straightforward to convert one into the other, though; there’s even an online tool available to do so here.

Add this

earthGeometry.firstMaterial!.normal.contents = earthBump

EDIT: the updated code will be:

let earthGeometry = SCNSphere(radius: radius)

let earthMap = UIImage(named: "Earth_map") let earthBump = UIImage(named: "Earth_bump") let earthSpec = UIImage(named: "Earth_specular")

earthGeometry.firstMaterial!.diffuse.contents = earthMap earthGeometry.firstMaterial!.specular.contents = earthSpec earthGeometry.firstMaterial!.normal.contents = earthBump

let earth = SCNNode(geometry: earthGeometry)

  • 1
    Hi Sarbartha, and welcome to GameDev.StackExchange! Users here usually like detailed answers, so you might find you get more upvotes by editing your answer to add a little explanation of why this solution works. For simple definitions, a short excerpt from the documentation can help. – DMGregory Dec 3 '17 at 14:10

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.