• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
football94

Shadow Casting animated model

1 post in this topic

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

[attachment=18898: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 ) ;

[attachment=18899: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

0

Share this post


Link to post
Share on other sites

Finally got it working !!biggrin.png

 

 

if anyone is interested code and pics below

 

 

needs size adjustments but working

[attachment=18912:11 23 2013 9 19 00 AM.jpeg]

 

 

[attachment=18913:11 23 2013 9 19 06 AM.jpeg]

 

 

[attachment=18914: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);

       
        }
0

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  
Followers 0