MTLTexture to CIImage conversion generates wrong colors

Hi all,
I have an array of MTLTexture, that comes from my Renderer draw method.
When I get MTLTexture, I want to use it as SCNMaterial diffuse.contents in a SCNScene, and after that, export it as CIImage and then to jpeg/png for the web.

In order to easily explain the problem I exported 3 images.

  1. Original MTLTexture

  2. CIImage from MTLTexture

  3. UIImage from CIImage

PROBLEM:

I get different colors, as you can see, for my blue line. The first picture is the line in MTLTexture as I intended it to be, and is generated by the overlapping of a very high number of dots with very low alpha.
In the second and third images for some reason the lines have a low alpha as if the dot blending is not additional.

The alpha channel is calculated in the shader’s fragment function like this:

baseColor.a = color.a / overlapping;

where overlapping means how many blue dots whit same alpha overlap per space unit.

If I set “baseColor.a = 1.0;”, so no alpha channel is intended to use in my texture, then the result is what I expect and MTLTexture CIImage and UIImage coincide.

For any other values of alpha, I get the error described above.

Any ideas on how I can solve this problem?

From this information, I don’t know why you are getting this result. Your center image seems to have alpha = 0. Do you have blending enabled? Do you have the same colorspace for all of them?

I haven’t had a huge amount of experience with Core Image, so I don’t know that I would know the answer even if I saw all the code.

Hi @caroline, thank for your reply.

The flow I would want to implement, is to export 6 MTLTexture and set them as SCNBox faces.
As Material.diffuse.contents can be a MTLTexture, I tried to set directly my textures and as a result I obtain an image like image 3 (UIImage from CIImage).

Not happy I searched for a solution and I found that the blend option in my SCNScene was not set correctly: now I set it to “multiply”.

With .multiply set, the color is almost correct, but darker.

rendered

My color space is bgra8Unorm everywhere in Metal space, creating CIImage I set CGColorSpaceCreateDeviceRGB() as colorspace.

My aim is to export MTLTexture as PNG file with the same color setting I get in Metal.