Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scenekit some textures have a red hue

I have a scene that has many objects, that all have different textures. For some reason 2 objects have a red hue to them, even though their textures have no red. You can still see the pattern in the texture, it just has different shades on red. (On the simulator the 2 objects have black and white textures and on device shades of red) Does anyone have any idea why this is happening? Other objects are working fine.

like image 440
Colddog Avatar asked Oct 15 '16 11:10

Colddog


2 Answers

For material properties such as metalness and roughness SceneKit has support for 1 channel (grayscale) images. In order to save memory these images are kept as grayscale textures, they are not converted to RGB textures that have the same data in each channel.

When you use such an image for a "coloured" material property (such as diffuse), SceneKit will ask for the red, green and blue components of the sample but green and blue will always be 0 and the image will appear red.

One unfortunate solution consists in reworking your texture in an image editing app so that it's saved as RBG instead of grayscale.

You can also try shader modifiers to convert from grayscale to RGB:

_surface.diffuse.rgb = _surface.diffuse.rrr;

Edit

Starting iOS 11 you can use the textureComponents property when using single channel textures:

material.diffuse.textureComponents = .red
like image 141
mnuages Avatar answered Nov 25 '22 16:11

mnuages


I've had the same problem with loading (grey) .png images as texture for a SCNMaterial.

On the simulator it rendered fine. But on the device all images with only black, white or grey, renders with a red base color.

let texture = SCNMaterial()
texture.diffuse.contents = UIImage(named: "rgb image with only gray")

I solved this problem by creating a new UIImage from the given UIImage:

let texture = SCNMaterial()
var newImage: UIImage?
if let textureImage = UIImage(named: "rgb image with only gray") {
    UIGraphicsBeginImageContext(textureImage.size)
    let width = textureImage.size.width
    let height = textureImage.size.height
    textureImage.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
    newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}
texture.diffuse.contents = newImage

HTH

like image 29
Dirk van Oosterbosch Avatar answered Nov 25 '22 15:11

Dirk van Oosterbosch