简体   繁体   中英

Image texture in SCNMaterial is always grey. How to apply color?

I am writing an image recognition app by following this example from Apple documentation: https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience

Everything works fine so far, now i'd like to change the transparent plane overlay to an image. I set the material of the plane like this.

let image = "overlay"
let material = SCNMaterial()
material.locksAmbientWithDiffuse = true;
material.isDoubleSided = false
material.diffuse.contents = image
material.ambient.contents = UIColor.white

let planeNode = SCNNode(geometry: plane)
planeNode.geometry?.materials = [material]

The image is loaded and rendered, only that it is always lacking the color. Original image is red, the overlay is b/w. I already tried many different settings for ambient and diffuse without success. What am i missing here?

Thanks

In your code the following line is a String type, not UIImage type:

let image: String = "overlay"

Try the following code:

let plane = SCNPlane(width: 10, height: 10)

let image = UIImage(named: "art.scnassets/texture")
// let image = UIColor.red
// let image = UIColor(hue: 0.25, saturation: 0.5, brightness: 0.75, alpha: 1)

let material = SCNMaterial()
material.locksAmbientWithDiffuse = true
material.isDoubleSided = false
material.diffuse.contents = image
material.ambient.contents = UIColor.white

let planeNode = SCNNode(geometry: plane)
planeNode.geometry?.materials = [material]

scene.rootNode.addChildNode(planeNode)

Also, save your image for diffuse material slot as PNG format.

在此处输入图像描述

For future reference, here is what i found out: Images shall not have the same name, even when saved in different groups or folders. I was using the name of the image as reference to load an overlay. When i changed the name and got the reference manually (by switch/case) everything works just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM