Ok, so I ported the game that I worked on in Monogame, but now I have a problem with the shader when it is ported. This is a strange mistake, since it works on my old XNA project, and it also works the first time I use it in a new monogame project, but not after that if I do not restart the game.
A shader is a very simple shader that looks at a grayscale image and extracts color from the search texture based on gray. I mainly use this to randomize an enemy sprite image every time a new enemy is placed on the screen. It works the first time the enemy spawns, but after that it doesnβt work, just giving a completely transparent texture (rather than a null texture).
In addition, so far I focus only on Windows Desktop, but at some point I plan to target Mac and Linux.
Here is the shader code itself.
sampler input : register(s0); Texture2D colorTable; float seed; //calculate in program, pass to shader (between 0 and 1) sampler colorTableSampler = sampler_state { Texture = <colorTable>; }; float4 PixelShaderFunction(float2 c: TEXCOORD0) : COLOR0 { //get current pixel of the texture (greyscale) float4 color = tex2D(input, c); //set the values to compare to. float hair = 139/255; float hairless = 140/255; float shirt = 181/255; float shirtless = 182/255; //var to hold new color float4 swap; //pixel coordinate for lookup float2 i; iy = 1; //compare and swap if (color.r >= hair && color.r <= hairless) { ix = ((0.5 + seed + 96)/128); swap = tex2D(colorTableSampler,i); } if (color.r >= shirt && color.r <= shirtless) { ix = ((0.5 + seed + 64)/128); swap = tex2D(colorTableSampler,i); } if (color.r == 1) { ix = ((0.5 + seed + 32)/128); swap = tex2D(colorTableSampler,i); } if (color.r == 0) { ix = ((0.5 + seed)/128); swap = tex2D(colorTableSampler, i); } return swap; } technique ColorSwap { pass Pass1 { // TODO: set renderstates here. PixelShader = compile ps_2_0 PixelShaderFunction(); } }
And here is the function that creates the texture. I should also note that texture generation works fine without a shader, I just get the basic image in grayscale.
public static Texture2D createEnemyTexture(GraphicsDevice gd, SpriteBatch sb) {
And just in case, the code to load in the shader:
BinaryReader Reader = new BinaryReader(File.Open(@"Content\\shaders\\test.mgfx", FileMode.Open)); colorSwapEffect = new Effect(gd, Reader.ReadBytes((int)Reader.BaseStream.Length));
If anyone has any ideas to fix this, I would really appreciate it, and just let me know if you need other information about the problem.