3
\$\begingroup\$

Our engine uses deferred rendering and at the main draw phase gathers plenty of data from the objects it draws.

In order to save on tex2D calls, we packed our objects' specular maps with all sorts of data, so three out of four channels are already taken. To make it clear: I am talking about the assets that come with the models and are stored in their material's Specular Level channel, not about the RenderTarget. So now I need another information to be stored in the alpha channel, but I cannot make the shader to read it properly! Nomatter what I write into alpha it ends up being 1 (255)! I tried:

  1. saving the textures in PNG/TGA formats.

  2. turning off pre-computed alpha in model's properties.

Out of every texture available to me (we use Diffuse map, Normal Map and Specular Map) I was only able to read alpha successfully from the Diffuse Map!

Here is how I add specular and normal maps to my model's material in the content processor:

if (geometry.Material.Textures.ContainsKey(normalMapKey))
{
    ExternalReference<TextureContent> texRef = geometry.Material.Textures[normalMapKey];
    geometry.Material.Textures.Remove("NormalMap");
    geometry.Material.Textures.Add("NormalMap", texRef);
}

...

foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture
in material.Textures)
{
    if ((texture.Key == "Texture") ||
        (texture.Key == "NormalMap") ||
        (texture.Key == "SpecularMap"))
        mat.Textures.Add(texture.Key, texture.Value);
}

In the shader I obviously use:

float4 data = tex2D(specularMapSampler, TexCoords);

so data.a is always 1 in my case, could you suggest a reason?

\$\endgroup\$
  • \$\begingroup\$ I've found that a lot of applications do funky things with alpha when saving (e.g. if alpha is 0 colour data is discarded for that pixel). Are you absolutely certain the data being saved into your files is what you think it is? \$\endgroup\$ – Martin Oct 29 '13 at 17:55
  • \$\begingroup\$ I have no idea how to make sure it does other than open the texture's channels in Photoshop. The latter seems to show alpha stored the way I intended it to. I did other alpha manipulations with the channel map and they all worked noticeably. I know that in png alpha is stored in a peculiar way and that XNA 4 premultiplies alphas but I accounted for these in my experiments. I also mentioned that I managed to successfully write and read alpha from the main model's texture (the diffuse channel) so I really doubt the problem is in the way I write textures. \$\endgroup\$ – cubrman Oct 30 '13 at 7:56
1
\$\begingroup\$

Ok I returned to this question now as our engine started requiring more channels and finally I solved it. I am using a custom content processor, and in such a case the user should manually specify whether he needs premultiplied alpha in the content processor. Or so I concluded :). Basically I added this code into my ConvertMaterial() method:

OpaqueDataDictionary processorParameters = new OpaqueDataDictionary();
            processorParameters["ColorKeyColor"] = this.ColorKeyColor;
            processorParameters["ColorKeyEnabled"] = false;
            processorParameters["TextureFormat"] = this.TextureFormat;
            processorParameters["GenerateMipmaps"] = this.GenerateMipmaps;
            processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo;
            processorParameters["PremultiplyTextureAlpha"] = false;
            processorParameters["ColorKeyEnabled"] = false;

            return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name,
                processorParameters);

and I could finally read alpha from normal and specular maps, while not having them messed up by alpha. Just remember to never store alpha == 0 as this WILL ruin the other channes and I don't know how to override this.

P.S. I got this working with .tga, did not try other formats.

\$\endgroup\$
0
\$\begingroup\$

It's hard to say without seeing all the code. Just guessing here, but what format are you using for your Specular and Normal maps? Are they 4 component formats?

\$\endgroup\$
  • \$\begingroup\$ I tried PNG and TGA - none worked. Naturally I did not try jpg. \$\endgroup\$ – cubrman Sep 24 '13 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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