1
\$\begingroup\$

I created an effect and just want to use lightning but in default effect that XNA create we should do texture mapping or the model appears 'RED', because of this lines of code in the effect file:

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 output = float4(1,0,0,1);

return output;
}

If I want to see my model, I must do texture mapping by UV coordinates. But my model does not have UV coordinates assigned or its UV coordinates are not exported.

I do texture mapping by this line of code in vertexshaderfunction:

 output.UV= input.UV 

When I use BasicEffect I have no problem and model appears correctly.

How can I use "just" lightings in my custom effects?

here is inside of my Model Using BasicEffect

enter image description here

This is my code for drawing with or without BasicEffect inside of my draw() method:

    Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z)
            * Matrix.CreateTranslation(Position);

        foreach(ModelMesh mesh in Model.Meshes)
        {
            Matrix localWorld = ModelTransforms[mesh.ParentBone.Index] * baseWorld;

            foreach(ModelMeshPart part in mesh.MeshParts)
            {
                Effect effect = part.Effect;

                if (effect is BasicEffect)
                {
                    ((BasicEffect)effect).World = localWorld;
                    ((BasicEffect)effect).View = View;
                    ((BasicEffect)effect).Projection = Projection;
                    ((BasicEffect)effect).EnableDefaultLighting();
                }
                else
                {
                    setEffectParameter(effect, "World", localWorld);
                    setEffectParameter(effect, "View", View);
                    setEffectParameter(effect, "Projection", Projection);
                    setEffectParameter(effect, "CameraPosition", CameraPosition);
                }
            }
            mesh.Draw();
        }

setEffectParameter is another method that sets effect parameter if i use my custom effect.

\$\endgroup\$
8
  • \$\begingroup\$ what do you mean, you have to use texture mapping? you can do whatever you want in your shader \$\endgroup\$
    – user13213
    Aug 8, 2012 at 7:48
  • \$\begingroup\$ Use different techniques, thats how basic effect does it. Also there is a code for basiceffect somewhere on app hub \$\endgroup\$
    – Kikaimaru
    Aug 8, 2012 at 7:50
  • \$\begingroup\$ mlak47: if i dont use texture mapping my model apears RED, how could i use lightning and dont use texture mapping? Kikiaimaru: could you tell me how? and what techniques? \$\endgroup\$
    – naprox
    Aug 8, 2012 at 7:52
  • 1
    \$\begingroup\$ Can you post a screenshot of how the model looks with BasicEffect? and can you post the relevant code that you use to set up BasicEffect. \$\endgroup\$ Aug 8, 2012 at 13:30
  • 1
    \$\begingroup\$ because i haven't enough reputation i must post another pic here: here is outside of my Model Using BasicEffect i.imgur.com/kIAtJ.jpg?1 \$\endgroup\$
    – naprox
    Aug 8, 2012 at 14:11

2 Answers 2

2
\$\begingroup\$

Effect has Techniques that tells you what vertex shader and pixel shader to use

In your .fx file you will have 1 vertex shader and 2 pixel shader functions, and two techniques

technique Colored
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader  = compile ps_2_0 ColoredPixelShader();
    }
}

technique Texture
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader  = compile ps_2_0 TexturedPixelShader();
    }
}

in Code you will then do

if ( /* model has texture coordinates */) // checking this can be complex at runtime but you can find it out from Model.Meshes[].MeshParts[].VertexBuffer.Declaration
{
    Effect.CurrentTechnique = Effect.Techniques["Textured"]
}
else
{
    Effect.CurrentTechnique = Effect.Techniques["Colored"]
}

Something similar is done by content pipeline on basic effect when importing Model, and thats where it should happen

\$\endgroup\$
2
\$\begingroup\$

Correct me if I wrong, but it looks like you are trying to mimic the BasicEffect shader code into your own shader file.

Microsoft's App Hub provides code for BasicEffect: http://xbox.create.msdn.com/en-US/education/catalog/sample/stock_effects

From there you can extract what you need, keep in mind that this will be cumbersome as BasicEffect encapsulates a lot of functionality.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .