Jump to content

  • Log In with Google      Sign In   
  • Create Account

Shadow Casting animated model


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 football94   Members   -  Reputation: 198

Like
0Likes
Like

Posted 22 November 2013 - 05:25 PM

HI everyone

Ive been experimenting with shadow casting using riemers tutorials as a guide(link below) and have been able to
to use it on still models but havent had much success on animated models, Im using the skinning sample from the xna website for
experimentation(link below) my question is do I make adjustments in the vertex shader of the skinned model sample or the shadow map and shadow scene vertex shaders if anyone could point me in the right direction as to the approach I would need to take it would be much
appreciated

                                                        Thankyou
 

 

 

riemers shadow link

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Real_shadow.php

 

 

skinning sample link

http://xbox.create.msdn.com/en-US/education/catalog/sample/skinned_model

 

 

 

this is what I have right now the still models are casting shadows but not the animated one

11 22 2013 1 19 43 PM.jpeg

skinned model hlsl

// Vertex shader input structure.
struct VS_INPUT
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
    float2 TexCoord : TEXCOORD0;
    float4 BoneIndices : BLENDINDICES0;
    float4 BoneWeights : BLENDWEIGHT0;
};


// Vertex shader output structure.
struct VS_OUTPUT
{
    float4 Position : POSITION0;
    float3 Lighting : COLOR0;
    float2 TexCoord : TEXCOORD0;
    
};


// Vertex shader program.
VS_OUTPUT VertexShader(VS_INPUT input)
{
    VS_OUTPUT output;
    
    // Blend between the weighted bone matrices.
    //float4x4 skinTransform = 0;
    
    skinTransform += Bones[input.BoneIndices.x] * input.BoneWeights.x;
    skinTransform += Bones[input.BoneIndices.y] * input.BoneWeights.y;
    skinTransform += Bones[input.BoneIndices.z] * input.BoneWeights.z;
    skinTransform += Bones[input.BoneIndices.w] * input.BoneWeights.w;
    
    // Skin the vertex position.
    float4 position = mul(input.Position,  skinTransform) ;
    
    
    
    
    output.Position = mul(position, xWorldViewProjection );
   
    
    
    
 
    
    
    
    

    // Skin the vertex normal, then compute lighting.
    float3 normal = normalize(mul(input.Normal, skinTransform));
    
    float3 light1 = max(dot(normal, Light1Direction), 0) * Light1Color;
    float3 light2 = max(dot(normal, Light2Direction), 0) * Light2Color;

    output.Lighting = light1 + light2 + AmbientColor;

    output.TexCoord = input.TexCoord;
    
    return output;
}


// Pixel shader input structure.
struct PS_INPUT
{
    float3 Lighting : COLOR0;
    float2 TexCoord : TEXCOORD0;
};


// Pixel shader program.
float4 PixelShader(PS_INPUT input) : COLOR0
{
    float4 color = tex2D(TextureSampler, input.TexCoord);

    color.rgb *= input.Lighting;
    
    return color;
}


technique SkinnedModelTechnique
{
    pass SkinnedModelPass
    {
        VertexShader = compile vs_2_0 VertexShader();
        PixelShader = compile ps_2_0 PixelShader();
    }
}

after playing around with the settings the best I could come up with is that

the animated model is casting a shadow but moved to a position in the

lower left hand corner of the screen

 

portion of code that was changed

output.Position =  mul(position,xlightsWorld ) ;

11 22 2013 1 21 35 PM.jpeg

skinned model hlsl

// Vertex shader input structure.
struct VS_INPUT
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
    float2 TexCoord : TEXCOORD0;
    float4 BoneIndices : BLENDINDICES0;
    float4 BoneWeights : BLENDWEIGHT0;
};


// Vertex shader output structure.
struct VS_OUTPUT
{
    float4 Position : POSITION0;
    float3 Lighting : COLOR0;
    float2 TexCoord : TEXCOORD0;
    
};


// Vertex shader program.
VS_OUTPUT VertexShader(VS_INPUT input)
{
    VS_OUTPUT output;
    
    // Blend between the weighted bone matrices.
    //float4x4 skinTransform = 0;
    
    skinTransform += Bones[input.BoneIndices.x] * input.BoneWeights.x;
    skinTransform += Bones[input.BoneIndices.y] * input.BoneWeights.y;
    skinTransform += Bones[input.BoneIndices.z] * input.BoneWeights.z;
    skinTransform += Bones[input.BoneIndices.w] * input.BoneWeights.w;
    
    // Skin the vertex position.
    float4 position = mul(input.Position,  skinTransform) ;
    
    
    
    output.Position =  mul(position,xlightsWorld ) ;
    
   
    
    
    
 
    
    
    
    

    // Skin the vertex normal, then compute lighting.
    float3 normal = normalize(mul(input.Normal, skinTransform));
    
    float3 light1 = max(dot(normal, Light1Direction), 0) * Light1Color;
    float3 light2 = max(dot(normal, Light2Direction), 0) * Light2Color;

    output.Lighting = light1 + light2 + AmbientColor;

    output.TexCoord = input.TexCoord;
    
    return output;
}


// Pixel shader input structure.
struct PS_INPUT
{
    float3 Lighting : COLOR0;
    float2 TexCoord : TEXCOORD0;
};


// Pixel shader program.
float4 PixelShader(PS_INPUT input) : COLOR0
{
    float4 color = tex2D(TextureSampler, input.TexCoord);

    color.rgb *= input.Lighting;
    
    return color;
}


technique SkinnedModelTechnique
{
    pass SkinnedModelPass
    {
        VertexShader = compile vs_2_0 VertexShader();
        PixelShader = compile ps_2_0 PixelShader();
    }
}



Shadow map hlsl

struct SMapVertexToPixel
 {
     float4 Position     : POSITION;
     float4 Position2D    : TEXCOORD0;
 };
 
 struct SMapPixelToFrame
 {
     float4 Color : COLOR0;
 };
 
 //xMaxDepth;
 SMapVertexToPixel ShadowMapVertexShader( float4 inPos : POSITION)
 {
     SMapVertexToPixel Output = (SMapVertexToPixel)0;
 
     Output.Position = mul(inPos, xlightsWorld);
     Output.Position2D = Output.Position;
 
     return Output;
 }
 
 SMapPixelToFrame ShadowMapPixelShader(SMapVertexToPixel PSIn)
 {
     SMapPixelToFrame Output = (SMapPixelToFrame)0;            
 
     Output.Color = PSIn.Position2D.z/PSIn.Position2D.w;
 
     return Output;
 }
 
 
 technique ShadowMap
 {
     pass Pass0
     {
         VertexShader = compile vs_2_0 ShadowMapVertexShader();
         PixelShader = compile ps_2_0 ShadowMapPixelShader();
     }
 }

Shadowedscene hlsl

struct SSceneVertexToPixel
 {
     float4 Position             : POSITION;
     float4 Pos2DAsSeenByLight    : TEXCOORD0;
     float2 TexCoords            : TEXCOORD1;
     float3 Normal                : TEXCOORD2;
     float4 Position3D            : TEXCOORD3;
     
 };
 
 struct SScenePixelToFrame
 {
     float4 Color : COLOR0;
 };
 
 
 float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
    float3 lightDir = normalize(pos3D - lightPos);
    return dot(-lightDir, normal);    
}
 
 SSceneVertexToPixel ShadowedSceneVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0, float3 inNormal : NORMAL)
 {
     SSceneVertexToPixel Output = (SSceneVertexToPixel)0;
 
     Output.Position = mul(inPos, xWorldViewProjection); 
        
     Output.Pos2DAsSeenByLight = mul(inPos, xlightsWorld);    
     Output.Normal = normalize(mul(inNormal, (float3x3)xWorld));    
     Output.Position3D = mul(inPos, xWorld);
     Output.TexCoords = inTexCoords;  
     
     
     
     
     
       
 
     return Output;
 }
 
 SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn)
 {
     SScenePixelToFrame Output = (SScenePixelToFrame)0;    
 
     float2 ProjectedTexCoords;
     ProjectedTexCoords[0] = PSIn.Pos2DAsSeenByLight.x/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
     ProjectedTexCoords[1] = -PSIn.Pos2DAsSeenByLight.y/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
     
     float diffuseLightingFactor = 0;
     if ((saturate(ProjectedTexCoords).x == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords).y == ProjectedTexCoords.y))
     {
         float depthStoredInShadowMap = tex2D(ShadowMapSampler, ProjectedTexCoords).r;
         float realDistance = PSIn.Pos2DAsSeenByLight.z/PSIn.Pos2DAsSeenByLight.w;
         if ((realDistance - 1.0f/100.0f) <= depthStoredInShadowMap)
         {
             diffuseLightingFactor = DotProduct(xLightPosition, PSIn.Position3D, PSIn.Normal);
             diffuseLightingFactor = saturate(diffuseLightingFactor);
             diffuseLightingFactor *= xLightPower;            
         }
     }
         
     float4 baseColor = tex2D(TextureSampler, PSIn.TexCoords);                
     Output.Color = baseColor*(diffuseLightingFactor + xAmbient);
 
     return Output;
 }


 
 technique ShadowedScene
 {
     pass Pass0
     {
         VertexShader = compile vs_2_0 ShadowedSceneVertexShader();
         PixelShader = compile ps_2_0 ShadowedScenePixelShader();
     }
 }

constants

//------- Constants --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float4x4 xlightsWorld;
float3 xLightDirection;
float xAmbient;
float xMaxDepth;
float xLightPower;
float3 xLightPosition;
float4x4 xWorldViewProjection;

  Sorry for so much code just wasnt quite sure what to include

 

                                    Thanks again



Sponsor:

#2 football94   Members   -  Reputation: 198

Like
0Likes
Like

Posted 23 November 2013 - 11:44 AM

Finally got it working !!biggrin.png

 

 

if anyone is interested code and pics below

 

 

needs size adjustments but working

11 23 2013 9 19 00 AM.jpeg

 

 

11 23 2013 9 19 06 AM.jpeg

 

 

11 23 2013 9 19 17 AM.jpeg

 

 

adjustments made to riemers hlsl code

 

shadowmap

struct SMapVertexToPixel1
 {
     float4 Position     : POSITION;
     float4 Position2D    : TEXCOORD0;
 };
 
 struct SMapPixelToFrame1
 {
     float4 Color : COLOR0;
 };
 
 //xMaxDepth;
 SMapVertexToPixel1 ShadowMapVertexShader1( float4 inPos : POSITION,float4 BoneIndices : BLENDINDICES0,float4 BoneWeights : BLENDWEIGHT0)
 {
     SMapVertexToPixel1 Output = (SMapVertexToPixel1)0;
     
      skinTransform += Bones[BoneIndices.x] * BoneWeights.x;
    skinTransform += Bones[BoneIndices.y] * BoneWeights.y;
    skinTransform += Bones[BoneIndices.z] * BoneWeights.z;
    skinTransform += Bones[BoneIndices.w] * BoneWeights.w;
     
     float4 position = mul(inPos, skinTransform);
    
   
     Output.Position = mul(position, xlightsWorld);
    
     Output.Position2D = Output.Position;
    
     
     
     
   
     
     
 
     return Output;
 }
 
 SMapPixelToFrame1 ShadowMapPixelShader1(SMapVertexToPixel1 PSIn)
 {
     SMapPixelToFrame1 Output = (SMapPixelToFrame1)0;            
 
     Output.Color = PSIn.Position2D.z/PSIn.Position2D.w;
 
     return Output;
 }
 
 
 technique dudeshadow
 {
     pass Pass0
     {
         VertexShader = compile vs_2_0 ShadowMapVertexShader1();
         PixelShader = compile ps_2_0 ShadowMapPixelShader1();
     }
 }
 

ShadowedScene

struct SSceneVertexToPixel1
 {
     float4 Position             : POSITION;
     float4 Pos2DAsSeenByLight    : TEXCOORD0;
     float2 TexCoords            : TEXCOORD1;
     float3 Normal                : TEXCOORD2;
     float4 Position3D            : TEXCOORD3;
     
 };
 
 struct SScenePixelToFrame1
 {
     float4 Color : COLOR0;
 };
 
 
 
 
 SSceneVertexToPixel1 ShadowedSceneVertexShader1( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0, float3 inNormal : NORMAL,float4 BoneIndices : BLENDINDICES0,float4 BoneWeights : BLENDWEIGHT0)
 {
     SSceneVertexToPixel1 Output = (SSceneVertexToPixel1)0;
     
    
      
       skinTransform += Bones[BoneIndices.x] * BoneWeights.x;
    skinTransform += Bones[BoneIndices.y] * BoneWeights.y;
    skinTransform += Bones[BoneIndices.z] * BoneWeights.z;
    skinTransform += Bones[BoneIndices.w] * BoneWeights.w;
     
    
     
     
     
     float4 position = mul(inPos, skinTransform);
     
      Output.Position = mul(position, xWorldViewProjection);
      Output.Pos2DAsSeenByLight = mul(position, xlightsWorld); 
     Output.Normal = normalize(mul(inNormal, skinTransform));
  Output.Position3D = mul(position, xWorld);
     
     Output.TexCoords = inTexCoords;  
     
     
     
    
    
     
     
     
     
       
 
     return Output;
 }
 
 SScenePixelToFrame1 ShadowedScenePixelShader1(SSceneVertexToPixel1 PSIn)
 {
     SScenePixelToFrame1 Output = (SScenePixelToFrame1)0;    
 
     float2 ProjectedTexCoords;
     ProjectedTexCoords[0] = PSIn.Pos2DAsSeenByLight.x/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
     ProjectedTexCoords[1] = -PSIn.Pos2DAsSeenByLight.y/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
     
     float diffuseLightingFactor = 0;
     if ((saturate(ProjectedTexCoords).x == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords).y == ProjectedTexCoords.y))
     {
         float depthStoredInShadowMap = tex2D(ShadowMapSampler, ProjectedTexCoords).r;
         float realDistance = PSIn.Pos2DAsSeenByLight.z/PSIn.Pos2DAsSeenByLight.w;
         if ((realDistance - 1.0f/100.0f) <= depthStoredInShadowMap)
         {
             diffuseLightingFactor = DotProduct(xLightPosition, PSIn.Position3D, PSIn.Normal);
             diffuseLightingFactor = saturate(diffuseLightingFactor);
             diffuseLightingFactor *= xLightPower;            
         }
     }
         
     float4 baseColor = tex2D(TextureSampler, PSIn.TexCoords);                
     Output.Color = baseColor*(diffuseLightingFactor + xAmbient);
 
     return Output;
 }


 
 technique dudeScene
 {
     pass Pass0
     {
         VertexShader = compile vs_2_0 ShadowedSceneVertexShader1();
         PixelShader = compile ps_2_0 ShadowedScenePixelShader1();
     }
 }

xna code

draw dude

  void Drawdude(string technique)
        {
           






            Vector3 lightPos5 = new Vector3(8, 0, -10);
           

             bones = animationPlayer.GetSkinTransforms();
            for (int i = 0; i < bones.Length; i++)
            {
                bones[i] *=
                      Matrix.CreateScale(0.30f,0.30f,0.30f)
                       
                      *Matrix.CreateFromQuaternion(RotationQuat)
                    * Matrix.CreateTranslation(lightPos5)
                    * Matrix.CreateWorld(Translation, Vector3.Forward, Vector3.Up); //Move the models position
            }




            Matrix wMatrix = Matrix.CreateTranslation(Position3);
            Matrix[] modelTransforms = new Matrix[currentModel.Bones.Count];
            currentModel.CopyAbsoluteBoneTransformsTo(modelTransforms);

            // Render the skinned mesh.
            foreach (ModelMesh mesh in currentModel.Meshes)
            {
                foreach (Effect effect in mesh.Effects)
                {
                    Matrix worldMatrix = modelTransforms[mesh.ParentBone.Index];


                    effect.CurrentTechnique = effect.Techniques[technique];
                    effect.Parameters["Bones"].SetValue(bones);
                    effect.Parameters["xTexture"].SetValue(clothes);
                    effect.Parameters["xWorld"].SetValue(worldMatrix);
                    effect.Parameters["xLightPower"].SetValue(lightPower);
                    effect.Parameters["xWorldViewProjection"].SetValue(Matrix.Identity * camera.viewMatrix * camera.projectionMatrix);
                    effect.Parameters["xLightPosition"].SetValue(lightPos5);
                    effect.Parameters["xAmbient"].SetValue(ambientPower);
                    effect.Parameters["xlightsWorld"].SetValue(lightsViewProjectionMatrix);
                    effect.Parameters["xShadowMap"].SetValue(shadowMap);
                   
                     

                }

                mesh.Draw();
                

            }
        }

draw code

  protected override void Draw(GameTime gameTime)
        {



            device.SetRenderTarget(0, renderTarget);
            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);


            DrawCity("ShadowMap");
            Drawdude("dudeshadow");


            device.SetRenderTarget(0, null);
           
            shadowMap = renderTarget.GetTexture();


            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

            
            DrawCity("ShadowedScene");
            Drawdude("dudeScene");
            
            // draw shadow map in top-left corner
            //SpriteBatch sprite = new SpriteBatch(device);
            //sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            //sprite.Draw(shadowMap, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 0.6f, SpriteEffects.None, 1);
           //sprite.End();

           

           

            base.Draw(gameTime);

       
        }





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS