Jump to content

  • Log In with Google      Sign In   
  • Create Account

-XM-

Member Since 17 Nov 2007
Offline Last Active Oct 16 2014 08:21 AM

Topics I've Started

MonoGame/HLSL Seperate Texture from sampler

29 August 2014 - 08:27 AM

For the platform I'm targeting with MonoGame, I'm needing to replace all tex2d calls with a call to Sample from a Texture2D object.

 

Right now I'm trying to update my shaders in the PC/XNA4 version of my game. I'm trying to update the BloomCombine.fx shader from the XNA Bloom sample but all I'm seeing right now is a black screen.

 

The culprit is these lines:

    float4 bloom = bloomTexture.Sample(BloomSampler, texCoord);
    float4 base = baseTexture.Sample(BaseSampler, texCoord

 

I had to make new Texture2D objects, seperate from the samplers, but do I need to set these from my XNA code? I'm setting them to a register, so to my understanding it should work fine, just like how the samplers are set to a register.

 

Here's what I have so far:

// Pixel shader combines the bloom image with the original
// scene, using tweakable intensity levels and saturation.
// This is the final step in applying a bloom postprocess.
SamplerState BloomSampler : register(s0);
SamplerState BaseSampler : register(s1);

Texture2D<float4> bloomTexture : register(t0); 
Texture2D<float4> baseTexture : register(t1);

float BloomIntensity;
float BaseIntensity;

float BloomSaturation;
float BaseSaturation;

// Helper for modifying the saturation of a color.
float4 AdjustSaturation(float4 color, float saturation)
{
    // The constants 0.3, 0.59, and 0.11 are chosen because the
    // human eye is more sensitive to green light, and less to blue.
    float grey = dot(color, float4(0.3, 0.59, 0.11, 1));

    return lerp(grey, color, saturation);
}

float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
    // Look up the bloom and original base image colors.
    //float4 bloom = tex2D(BloomSampler, texCoord);
    //float4 base = tex2D(BaseSampler, texCoord); << Old code
	float4 bloom = bloomTexture.Sample(BloomSampler, texCoord);
	float4 base = baseTexture.Sample(BaseSampler, texCoord);
    
    // Adjust color saturation and intensity.
    bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
    base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
    
    // Darken down the base image in areas where there is a lot of bloom,
    // to prevent things looking excessively burned-out.
    base *= (1 - saturate(bloom));
    
    // Combine the two images.
    return base + bloom;
}

technique BloomCombine
{
    pass Pass1
    {
#if SM4
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
#elif SM3
        PixelShader = compile ps_3_0 PixelShaderFunction();
#else
        PixelShader = compile ps_2_0 PixelShaderFunction();
#endif
    }
}


8-Direction 2D Animations Flicker

22 September 2013 - 11:06 AM

Hi guys,

 

I'm working on a 2.5D game (think top down dungeon crawler/side view like Binding of Isaac) where characters can walk in all eight directions. The player animations work fine, but the AI animations have been flickering quite a bit between movement states. The AI moves and selects an animation based on its direction:


            //Select which animation we should currently be in:
            if (velocity != Vector2.Zero)
            {
                if (direction.X < 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Left")
                    ChangeAnimation(Animations.WalkUpLeft);
                else if (direction.X > 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Right")
                    ChangeAnimation(Animations.WalkUpRight);
                else if (direction.X < 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Left")
                    ChangeAnimation(Animations.WalkDownLeft);
                else if (direction.X > 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Right")
                    ChangeAnimation(Animations.WalkDownRight);
                else if (direction.X < 0 && direction.Y == 0 && currentAnimation.name != "Walk Left")
                    ChangeAnimation(Animations.WalkLeft);
                else if (direction.X > 0 && direction.Y == 0 && currentAnimation.name != "Walk Right")
                    ChangeAnimation(Animations.WalkRight);
                else if (direction.Y > 0 && direction.X == 0 && currentAnimation.name != "Walk Down")
                    ChangeAnimation(Animations.WalkDown);
                else if (direction.Y < 0 && direction.X == 0 && currentAnimation.name != "Walk Up")
                    ChangeAnimation(Animations.WalkUp);
            }
            else if (velocity == Vector2.Zero)
            {
                if (currentAnimation.name != "Idle")
                    ChangeAnimation(Animations.Idle);
            }

The problem is that there are times when the AI has to move diagonally, and after doing so he will move straight down for a few frames to correct himself so he's fully on the grid... it's only for a few frames so visually it looks very glitchy going between the two directions.

 

The direction variable never has floating point values... The AI movement code is like this:

 

1. Calculate the next tile we need to move to.

2. Make our direction variable look at this tile.

3. Round so the direction is always a whole number, to avoid jerky movement from floating point numbers.

 

The code I use to round is this:

        private void TrimDirection()
        {
            //Snap our direction to one of the eight cardinal directions:
            direction.X = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.X / MathHelper.PiOver4));
            direction.Y = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.Y / MathHelper.PiOver4));
        }

Movement code is like this:

                if (direction.X > 0)
                    MoveRight();
                else if (direction.X < 0)
                    MoveLeft();

                if (direction.Y > 0)
                    MoveDown();
                else if (direction.Y < 0)
                    MoveUp();

I've been trying to figure out how to approach this issue and I've hit a wall.... do I need to implement some type of animation hysteresis to avoid animations from changing so fast? Or is this issue deeper down inside how I make the AI move?

 

Thanks in advance!


Dynamic 2D Shadows Optimization

13 February 2013 - 06:56 PM

Hi guys, I've implemented this shadow sample into my engine: http://www.catalinzima.com/2013/01/dynamic-2d-shadows-for-windows-phone-7/ Everything was working fine until I ran my game on the Xbox 360.... I get 30 FPS when my game runs at 1920 x 1080, but if I run it at 1280 x 720 or below it runs at 60 FPS. If you run the stand alone sample the same thing happens.

 

I don't know exactly why the resolution makes a difference, but I did some further profiling and found that this call gets called for every hull:

 

game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2);

 

When I draw just one hull my FPS goes back up to 60, so for the last week or so I've been trying to add all of the verts and indices to a list and then make this call one time after iterating through the hulls. The problem is that after doing this the shadows are drawing really whacky... I'm not sure where I'm going wrong, this should be a simple fix but it seems I'm overlooking something small that is screwing me up.

 

Any help would be appreciated! I attached the modified sample to this post.

 

My changes are in ConvexHull's DrawShadows:

            int currentIndex = startingIndex;
            int svCount = 0;
            while (svCount != shadowVertexCount * 2)
            {
                shadowHullIndices.Add(currentIndex + shadowHullVertices.Count); //Added this
                Vector3 vertexPos = vertices[currentIndex].Position + new Vector3(position, 0);

                //one vertex on the hull
                shadowVertices[svCount] = new VertexPositionColor();
                shadowVertices[svCount].Color = Color.Transparent;
                shadowVertices[svCount].Position = vertexPos;

                //one extruded by the light direction
                shadowVertices[svCount + 1] = new VertexPositionColor();
                shadowVertices[svCount + 1].Color = Color.Transparent;
                Vector3 L2P = vertexPos - new Vector3(lightSource.Position, 0);
                L2P.Normalize();
                shadowVertices[svCount + 1].Position = new Vector3(lightSource.Position, 0) + L2P * 9000;

                svCount += 2;
                currentIndex = (currentIndex + 1) % primitiveCount;
            }


            drawingEffect.World = Matrix.Identity;
            drawingEffect.CurrentTechnique.Passes[0].Apply();

            for (int i = 0; i < shadowVertexCount * 2 - 2; i++) //Added this
                shadowHullVertices.Add(shadowVertices[i]);
//Original call:
//game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2); 

and in Game1's DrawLightmap:

                foreach (ConvexHull ch in objects)
                {
                    //draw shadow
                    ch.DrawShadows(light, shadowHullVertices, shadowHullIndices); //Added. For now, pass the lists in and add the appropriate verts and indices.
                }

                //Added this:
                GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, shadowHullVertices.ToArray(), 0, shadowHullVertices.Count, shadowHullIndices.ToArray(), 0, shadowHullIndices.Count / 3);

 



 


Different Timing

04 October 2011 - 06:10 PM

Hey guys, my game is using a fixed time step.... so I was kind of under the impression that my game would run at the same speed no matter what platform is runs on. However, I have a cutscene that is based off of timers; when I watch the cutscene on my 360 it ends exactly when it's supposed to, but on my PC it's a second or two off. Is this a difference between Xbox and PC, or could it be that it would have different ending results on different PCs? I'm coding in XNA, and like I said, I'm using fixed time step so I'm kind of confused on why this is happening.

Thanks!

Getting Multiple Effects with Render Targets

26 April 2011 - 02:45 PM

Hey again everyone, I've come to a dead end in my recent coding attempts and thought I'd see if anyone here would be able to assist me. I am still working on a 3D space shooter in XNA, but am wanting to add some shader effects. I have taken the Bloom Sample from the XNA site and implemented it into my game.... with some modifications I have three different effect combinations that I'm wanting to apply. I want a different effect on my bullets, HUD and finally over the rest of the scene.

I have everything working great except getting all of these things to combine.... At first, I was using multiple render targets. One for each effect I wanted, after talking with a member at the XNA Forums, I realized this isn't a good way to do this because I will be losing depth information by using three textures like this. (Bullets would be showing on top of ships etc.). What I'd like to do is some how mark what needs a different effect and what doesn't so I can do all of this in one texture/render target. I have all the right code implemented, I just need some way to mark the bullets etc. How would I go about doing something like this?

Thanks guys!


PARTNERS