• Content count

  • Joined

  • Last visited

Community Reputation

120 Neutral

About derrace

  • Rank
  1. Anyway, I got it working. Thanks everyone for their help. Here's it in action =)
  2. [quote name='Steve_Segreto' timestamp='1353918109' post='5004145'] [quote name='derrace' timestamp='1353690938' post='5003531'] Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way. [/quote] I wonder if tinting the player would be good enough instead of a full palette swap ... don't use your existing palette swapping pixel shader, instead treat the image of your character as a greyscale by sampling the original texture and ignoring 2 out of the 3 color channels, then multiply the greyscale by a passed in tintcolor to make the player a different color. where you pass tintColor in as a uniform extern. You might have to experiment with which channel you want to use as the grey scale (r,g or b). [/quote] Hi Steve, changing my spritesheet to greyscale was a lot easier than I thought, although it's more of a Black and White filter (with same values for RGB). I had thought of that prior, but I have a problem that with my original palette, I don't have a unique RGB band, so sampling just one of the band is not possible. Selecting the average 2 of the 3 yields duplicate values, and it gets messy if i take the average of all 3. This was why I went with greyscale. My motivation for getting the palette swap working was because it would be reused in many situations. My palette has 15+ samples, so it would be a nightmare trying to get the right set of colours for try and error with the tinting method. Doing it with a colour table makes it easy to swap the colours, as well as knowing exactly what colours it would be swapping.
  3. [quote name='phil_t' timestamp='1353788243' post='5003807'] If you're using SpriteBatch, you have to specify the sampler state in the Begin call. Otherwise it will override it with LinearClamp. So use the overload of SpriteBatch.Begin that takes a SamplerState. [/quote] Ah, that did the trick. Thanks a lot! Are there any sites you could recommend to read up on this? The lack of documentation on MSDN is really slowing my progress and I am just stumbling along with random google searches.... =(
  4. [quote name='Kyall' timestamp='1353732212' post='5003666'] You might want to play around with the texture filtering for the sprite batch. Try adding this before the begin call: this.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; The default in XNA (according to the interlect) is LinearClamp [/quote] Hi there, I have tried that, but to no avail. I have been reading up on this: But I have no experience with sampler state. Do you know of any tutorials which might help? I am thinking of using the shader to do my scaling (magnification) just for the sprite. I am really at a lost atm =\
  5. Argh, turns out when my engine is running at 1x (default resolution) the shader works perfectly, any scale of that (including the default XNA's graphics.ToggleFullScreen();) stuffs it up =\. I am passing a transformation matrix into my spriteBtach.Begin method to scale my playable window by X amount, as well as using it for my camera + parallax effect. It appears that the translation or scale operations are messing up my pixel shader? I need help, badly now =(
  6. Alright, I have tried using greyscale as my index. Hmm, it looks a bit better after using greyscale, but still far from perfect.. Running out of ideas =\
  7. hmm, so I have added a check and it does look like a lot of pixels aren't being replaced from the lookup table. It almost appears like I got my indices wrong =\ I think I need a better way of indexing. Does anyone have any suggestion? To recap, currently I am taking the average of the RGB as the neither of the bands are unique =\. My palette (see screenshot attached) has potentially 15 pixels. [url=""][/url] Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way.
  8. [quote name='phil_t' timestamp='1353613221' post='5003300'] Texture coordinates range between (0, 1), not (0, n). Divide your index by n, where n is the number of texels (4 in your case). Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index. And make sure you're using point sampling. [/quote] Ah, 0 to 1. Got it. In my script, I don't think I need to do any division as it would probably have done it for me. When setting up my colourtable, I would just multiply it by 255 since I am using the bands of the pixel as my index. Got my algo for pixel shader for palette swap sorta working.. but I am having some issues with it. [url=""][/url] Bands aren't unique, so I am taking the average of RGB as my index. I don't get a whole number, but I made each lookup area 3x1 (i need to increase it to 3). I am not sure if it's the decimals that is causing the issue. I will need to debug further. Also, I have two default colours with the same value, but expecting different palette swaps.. How can this be achieved? for now just assume my colour table is 255 x 10. I will hardcode the dividers as well as the y index for now. [source lang="java"] float4 colour = tex2D(input, coords); float2 index = float2((colour.r + colour.g+colour.b)/3.0 , 4/10.0 + 0.5 / 10.0); float4 lookUp = tex2D(PaletteSampler,index); if(colour.a != 0) { colour.rgb = lookUp.rgb; } return colour; [/source] sorry, what do you mean by point sampling? Isn't the above point sampling?
  9. Ok, I have created a 4x1 png and inserted 4 different coloured pixels. I have changed index from 0 to 3 (or any value for that fact) but it always display the colour of the first pixel. If i change index with the var coords in the tex1D method, and move my sprite, it cycles through the colours when I move, but displays the colour of the first pixel when stationary. Do you have any idea why this is happening? here's the snippet of my function: [CODE] float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0 { float4 colour = tex2D(input, coords); int index = 0; //doesn't matter what value i put, it always return the first pixel of my palette float4 lookUp = tex1D(Palette2Sampler,index); //float4 lookUp = tex1D(Palette2Sampler,coord); // when I run the above and move the sprite, it cycles through the colours, but always // displays the colour of the first pixel when stationary. return lookup; } [/CODE]
  10. Hey all, thanks for the replies. [quote name='Kyall' timestamp='1353572011' post='5003164'] Use a 3D texture instead. That way instead of putting a for loop in your shader ( slow ), you take the color value, and use that value to look up a 3d texture that has the colours you want in it. [/quote] Hmm, that would require me to manually insert values into the 3rd dimension, if I have more more than 1 set of colours I want to change depending on the situation, I would need to store more than 1 look up value? [quote name='Hodgman' timestamp='1353573681' post='5003168'] If you're going to do this, it would be a whole lot simpler if the original sprite contained pallete-indices instead of colours. You could pre-process your sprites once on the CPU to avoid having to execute that ridiculously huge loop in your pixel shader. Then you'd just have to do "[font=courier new,courier,monospace]fetch sprite colour (which is an index), fetch colour from pallete at index[/font]", instead of "[font=courier new,courier,monospace]fetch sprite colour, search entire source pallets for colour to determine it's index, fetch colour from new pallete at index[/font]". [/quote] Similar to what you suggested, I am thinking along the line of using one of the 3 RGB channels as my unique index, and storing my colour table values in an array of 255 elements.
  11. Hmm, think I got it, It involves using a for loop to cycle through the a variant of the above code but I am getting this silly error now =( Shader uses texture addressing operations in a dependency chain that is too complex for the target shader model (ps_2_0) to handle.
  12. Hi there, I have implemented a simple pixel shader which can replace a particular colour in a sprite with another colour. It looks something like this: [CODE] sampler input : register(s0); float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0 { float4 colour = tex2D(input, coords); if(colour.r == sourceColours[0].r && colour.g == sourceColours[0].g && colour.b == sourceColours[0].b) return targetColours[0]; return colour; } [/CODE] What I would like to do is have the function take in 2 textures, a default table, and a lookup table (both same dimensions). Grab the current pixel, and find the location XY (coords) of the matching RGB in the default table, and then substitute it with the colour found in the lookup table at XY. I have figured how to pass the Textures from C# into the function, but I am not sure how to find the coords in the default table by matching the colour. Could someone kindly assist? Thanks in advance.
  13. 2D Camera, limiting bounds and zoom

    200+ views, can someone assist?
  14. Hi all, I have been following this tutorial ([url=""]http://www.david-gou...rolling-in-xna/[/url]) and I have build a 2D camera, but I am struggling to understand a particular behaviour of the camera when I am applying a zoom. For the sake of simplicity, I will be using 1x and 2x zooms to describe my query, my map size is 512x448 and at 1x, I am setting the res to 256x224, and 512x448 at 2x. I want to keep the amount of "objects" I can see consistent. I am using the zoom function instead of changing the scale in the draw method because I don't want to muck around with handling the scaling for the physics of the game. [code] graphics.PreferredBackBufferWidth = 256; graphics.PreferredBackBufferHeight = 224;[/code] [code] public void LookAt(Vector2 position) { Position = position - ScreenCenter; } [/code] In my update method, I have called LookAt and passed in the center position of my player object Is the above code meant to move the camera, only if the the player is not in the center of the screen? If I used the class without specifying a Limit object [code] cam = new Camera2d(GraphicsDevice.Viewport, ZoomScale); [/code] [attachment=11488:as1.png] From my understanding, David has implemented a Rectangle Limit object which I have used to specify my maximum map area along the lines: [code] cam = new Camera2d(GraphicsDevice.Viewport, ZoomScale) { Limits = new Rectangle(0, 0, (int)level.MapSize.X, (int)level.MapSize.Y) }; [/code] While I have managed to get it to work for 1x (+ panning) [attachment=11489:as2.png] Now, when I change it to 2X, I need to set the hard min limit for the Rect to (-128,-112) to ensure my camera displays exactly what the 1x displayed, only 2 times bigger. [code] else if (Zoom == 2.0f) //xxx hardcoded values for testing { pos.X = MathHelper.Clamp(pos.X, -128, Limits.Value.X + Limits.Value.Width - viewport.Width / Zoom); pos.Y = MathHelper.Clamp(pos.Y, -112, Limits.Value.Y + Limits.Value.Height - viewport.Height / Zoom); } [/code] [attachment=11490:as3.png] I have obtained the values by visually calculating the offset. My question is, why can't I set the limit's origin to (0,0)? Is the implementation in the tutorial wrong, or am I failing to understand how it works? The code is as follows: [code]class Camera2d { public Vector2 ScreenCenter; public float Zoom = 1; //default private readonly Viewport viewport; private Vector2 pos; private Rectangle? limits; public Vector2 Position { get { return pos; } set { pos = value; if (Limits != null) { if (Zoom == 1.0f) { pos.X = MathHelper.Clamp(pos.X, Limits.Value.X, Limits.Value.X + Limits.Value.Width - viewport.Width); pos.Y = MathHelper.Clamp(pos.Y, Limits.Value.Y, Limits.Value.Y + Limits.Value.Height - viewport.Height); } else if (Zoom == 2.0f) //xxx hardcoded values for testing { pos.X = MathHelper.Clamp(pos.X, -128, Limits.Value.X + Limits.Value.Width - viewport.Width / Zoom); pos.Y = MathHelper.Clamp(pos.Y, -112, Limits.Value.Y + Limits.Value.Height - viewport.Height / Zoom); } } } } public Rectangle? Limits { get { return limits; } set { if (value != null) { // Assign limit but make sure it's always bigger than the viewport limits = new Rectangle { X = value.Value.X, Y = value.Value.Y, Width = System.Math.Max(viewport.Width, value.Value.Width), Height = System.Math.Max(viewport.Height, value.Value.Height) }; // Validate camera position with new limit Position = Position; } else { limits = null; } } } public Camera2d(Viewport viewport, float zoom) { Zoom = zoom; this.viewport = viewport; ScreenCenter = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f); } public Matrix GetViewMatrix(Vector2 parallax) { return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) * Matrix.CreateTranslation(new Vector3(-ScreenCenter, 0.0f)) * Matrix.CreateScale(Zoom, Zoom, 1.0f) * Matrix.CreateTranslation(new Vector3(ScreenCenter, 0.0f)); } public void LookAt(Vector2 position) { Position = position - ScreenCenter; } public void Move(Vector2 displacement) { Position += displacement; } } [/code] Any inputs is much appreciated. Thank you for your time. ric