Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 17 Nov 2007
Offline Last Active Mar 03 2016 03:43 PM

Topics I've Started

Infinite Fractal

27 May 2015 - 12:03 PM

I've implemented the code here in my XNA project: http://blogs.msdn.com/b/shawnhar/archive/2006/12/12/technicolor-julias.aspx


I have a fractal drawing on the screen I can zoom in and out. However, when I zoom in to a certain amount the fractal ends and the pixels get distorted.


What I'm after is a fractal I can zoom into infinitely and it keeps giving me new patterns. Is this not possible to do in real time? If it's possible, can someone point me to good resources on how to do this in XNA4/HLSL? My google searches came up with nothing.



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();
        PixelShader = compile ps_2_0 PixelShaderFunction();

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")
                else if (direction.X > 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Right")
                else if (direction.X < 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Left")
                else if (direction.X > 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Right")
                else if (direction.X < 0 && direction.Y == 0 && currentAnimation.name != "Walk Left")
                else if (direction.X > 0 && direction.Y == 0 && currentAnimation.name != "Walk Right")
                else if (direction.Y > 0 && direction.X == 0 && currentAnimation.name != "Walk Down")
                else if (direction.Y < 0 && direction.X == 0 && currentAnimation.name != "Walk Up")
            else if (velocity == Vector2.Zero)
                if (currentAnimation.name != "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)
                else if (direction.X < 0)

                if (direction.Y > 0)
                else if (direction.Y < 0)

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);
                shadowVertices[svCount + 1].Position = new Vector3(lightSource.Position, 0) + L2P * 9000;

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

            drawingEffect.World = Matrix.Identity;

            for (int i = 0; i < shadowVertexCount * 2 - 2; i++) //Added this
//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.