Sign in to follow this  
VerMan

[Solved] XNA 3 - How to get material colors?

Recommended Posts

Hello, I've been playing around with XNA 3.0 and I'm stuck when trying to use my custom Phong Lighting shader because I can't get the proper color of the model, some models have textures and other don't, when I render using BasicEffect I can see the colors/textures but using my custom shader I can only see yellow diffuse color or B&W The first approach is passing the color from VS to PS
struct VSPhongIn
{
    float4 position : POSITION0;
    float3 normal   : NORMAL0;
    float4 color    : COLOR0;
};

struct VSPhongOut
{
    float4 position : POSITION0;
    float3 normal   : TEXCOORD0;
    float3 lightDir : TEXCOORD1;
    float3 viewDir  : TEXCOORD2;
    float4 color    : COLOR0;
};

VSPhongOut VSPhong(VSPhongIn input)
{
    VSPhongOut output;

    float4 worldPosition = mul(input.position, xWorld);
    float4 viewPosition = mul(worldPosition, xView);
    
    output.position = mul(viewPosition, xProjection);
    output.normal = mul(input.normal, xWorld);
    output.lightDir = xLightPosition - worldPosition.xyz;
    output.viewDir = xCameraPosition - worldPosition.xyz;
    output.color = input.color;
    
    return output;
}

float4 PSPhong(VSPhongOut input) : COLOR0
{
    float4 ambient = { 0.1f, 0.1f, 0.1f, 1.0f};

    float3 N = normalize(input.normal);
    float3 L = normalize(input.lightDir);
    float3 V = normalize(input.viewDir);
    float4 diff = saturate(dot(N, L)); //diffuse component

    // R = 2 * (N.L) * N - L
    float3 R = normalize(2 * diff * N - L);
    float4 specular = pow(saturate(dot(R, V)), 32);
    
    // I = Acolor + Dcolor * N.L + (R.V)n
    return ambient + input.color * diff + specular; 
}


And all I get is yellow... Then I tried to save the diffuse and textures from BasicEffect:
foreach (ModelMesh mesh in model.Meshes)
{
    foreach (ModelMeshPart part in mesh.MeshParts)
    {
        BasicEffect be = (BasicEffect)part.Effect;
        diffuseList.Add(be.DiffuseColor);
        textureList.Add(be.Texture);
        part.Effect = effect.Clone(device);
    }
}

diffuseArray = diffuseList.ToArray();
textureArray = textureList.ToArray();


and pass diffuse and texture to shader as a parameter. But I can't get the same color as when using BasicEffect. Any hints/ideas? [Edited by - VerMan on June 2, 2009 12:44:57 PM]

Share this post


Link to post
Share on other sites
What does your draw code look like when using your own shader?

BasicEffect is far from basic (well, it is quite basic, but not so much as you'd expect). I assume you've looked at the source for it for some hints to where you may be going wrong?

If not, it's here.

Share this post


Link to post
Share on other sites
thanks for the link...

I'm loading several models and for some reason, it's getting the diffuse and/or texture color of the first model loaded.

[Edited by - VerMan on June 2, 2009 11:52:55 AM]

Share this post


Link to post
Share on other sites
Ok, I was creating a unique arraylist for all the textures and I needed a new list for each model.

But the second approach was correct, copying diffuse & texture from BasicEffect for each mesh part.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this