c# - Mesh color is missing when using a custom effect, but not when using a BasicEffect -


this newbie question, i'm trying render fbx model colors (materials).

the thing is, when i've tried using basiceffect, went , colors rendered fine - writing custom hlsl effect file, , whenever try render model it, says model not have colors:

the current vertex declaration not include elements required current vertex shader. color0 missing.

it stupid semantic mistake (or that), since i'm not experienced working hlsl. anyway, here code (originally taken riemers tutorial):

float4x4 xworldviewprojection;  float4x4 xworld; float3 xlightpos; float xlightpower; float xambient;  struct vertextopixel {     float4 position     : position;         float3 normal        : texcoord0;     float3 position3d    : texcoord1;     float4 color        : color0; };  struct pixeltoframe {     float4 color        : color0; };  float dotproduct(float3 lightpos, float3 pos3d, float3 normal) {     float3 lightdir = normalize(pos3d - lightpos);         return dot(-lightdir, normal);     }  vertextopixel simplestvertexshader( float4 inpos : position0, float3 innormal: normal0, float4 incolor : color0) {     vertextopixel output = (vertextopixel)0;      output.position =mul(inpos, xworldviewprojection);     output.normal = normalize(mul(innormal, (float3x3)xworld));         output.position3d = mul(inpos, xworld);     output.color = incolor;      return output; }  pixeltoframe ourfirstpixelshader(vertextopixel psin) {     pixeltoframe output = (pixeltoframe)0;          float diffuselightingfactor = dotproduct(xlightpos, psin.position3d, psin.normal);     diffuselightingfactor = saturate(diffuselightingfactor);     diffuselightingfactor *= xlightpower;      output.color = psin.color* (diffuselightingfactor + xambient);      return output; }  technique simplest {     pass pass0     {         vertexshader = compile vs_3_0 simplestvertexshader();         pixelshader = compile ps_3_0 ourfirstpixelshader();     } } 

and xna code used:

(after loading:)

    foreach (modelmesh mesh in mainroom.meshes)         foreach (modelmeshpart meshpart in mesh.meshparts)             meshpart.effect = roomeffect.clone(); 

...

private void drawmodel(model model, matrix world) {     matrix[] bones = new matrix[model.bones.count];     model.copyabsolutebonetransformsto(bones);       foreach (modelmesh mesh in model.meshes)     {         foreach (effect currenteffect in mesh.effects)         {             matrix worldmatrix = bones[mesh.parentbone.index] * world;              roomeffect.currenttechnique = roomeffect.techniques["simplest"];             currenteffect.parameters["xworldviewprojection"].setvalue(worldmatrix * camera.view * camera.projection);             currenteffect.parameters["xworld"].setvalue(worldmatrix);             currenteffect.parameters["xlightpos"].setvalue(lightpos);             currenteffect.parameters["xlightpower"].setvalue(lightpower);             currenteffect.parameters["xambient"].setvalue(ambientpower);         }         mesh.draw();     } } 

happily enough, after reading both @andrewrussell 's comment , @cole campbell 's answer, have managed sort out problem (and few other bugs code had).

the final (working) code now:

float4x4 xworldviewprojection;  float4x4 xworld; float3 xlightpos; float xlightpower; float xambient; float3 diffusecolor;  struct vertextopixel {     float4 position      : position;         float3 normal        : texcoord0;     float3 position3d    : texcoord1;  };  struct pixeltoframe {     float4 color        : color0; };  float dotproduct(float3 lightpos, float3 pos3d, float3 normal) {     float3 lightdir = normalize(pos3d - lightpos);         return dot(-lightdir, normal);     }  vertextopixel simplestvertexshader(float4 inposition : position, float3 innormal : texcoord0) {     vertextopixel output = (vertextopixel)0;      output.position = mul(inposition, xworldviewprojection);     output.normal = normalize(mul(innormal, (float3x3)xworld));         output.position3d = mul(inposition, xworld);      return output; }  pixeltoframe ourfirstpixelshader(vertextopixel psin) {     pixeltoframe output = (pixeltoframe)0;          float diffuselightingfactor = dotproduct(xlightpos, psin.position3d, psin.normal);     diffuselightingfactor = saturate(diffuselightingfactor);     diffuselightingfactor *= xlightpower;      output.color = float4(diffusecolor, 1) * (diffuselightingfactor + xambient);      return output; }  technique simplest {     pass pass0     {         vertexshader = compile vs_3_0 simplestvertexshader();         pixelshader = compile ps_3_0 ourfirstpixelshader();     } } 

...

list<vector3> originaldiffusecolors = new list<vector3>(); 

...

protected override void loadcontent() {     ...      foreach (modelmesh mesh in mainroom.meshes)     {          foreach (basiceffect effect in mesh.effects)              originaldiffusecolors.add(effect.diffusecolor);          foreach (modelmeshpart meshpart in mesh.meshparts)              meshpart.effect = roomeffect.clone();     } } 

...

private void drawmodel(model model, matrix world) {     matrix[] bones = new matrix[model.bones.count];     model.copyabsolutebonetransformsto(bones);      roomeffect.currenttechnique = roomeffect.techniques["simplest"];      int count = 0;     foreach (modelmesh mesh in model.meshes)     {         foreach (effect currenteffect in mesh.effects)         {             matrix worldmatrix = bones[mesh.parentbone.index] * world;             currenteffect.parameters["xworldviewprojection"].setvalue(worldmatrix * camera.view * camera.projection);             currenteffect.parameters["xworld"].setvalue(worldmatrix);             currenteffect.parameters["xlightpos"].setvalue(lightpos);             currenteffect.parameters["xlightpower"].setvalue(lightpower);             currenteffect.parameters["xambient"].setvalue(ambientpower);             currenteffect.parameters["diffusecolor"].setvalue(originaldiffusecolors[count++]);         }         mesh.draw();     } } 

thanks help. =]


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -