gpt4 book ai didi

c# - Monogame 着色器移植问题

转载 作者:太空狗 更新时间:2023-10-29 20:41:54 26 4
gpt4 key购买 nike

好的,所以我将我一直在开发的游戏移植到 Monogame,但是现在移植后我遇到了着色器问题。这是一个奇怪的错误,因为它适用于我的旧 XNA 项目,而且我第一次在新的 monogame 项目中使用它时它也适用,但之后就不行了,除非我重新启动游戏。

着色器是一个非常简单的着色器,它查看灰度图像,并根据灰色从查找纹理中选择颜色。基本上,每次在屏幕上放置一个新敌人时,我都会使用它来随机化一个敌人的 Sprite 图像。它在第一次生成敌人时起作用,但之后不起作用,只是给出一个完全透明的纹理(不是空纹理)。

此外,我目前只针对 Windows 桌面,但我计划在某个时候针对 Mac 和 Linux。

这是着色器代码本身。

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;
i.y = 1;

//compare and swap
if (color.r >= hair && color.r <= hairless)
{
i.x = ((0.5 + seed + 96)/128);
swap = tex2D(colorTableSampler,i);
}
if (color.r >= shirt && color.r <= shirtless)
{
i.x = ((0.5 + seed + 64)/128);
swap = tex2D(colorTableSampler,i);
}
if (color.r == 1)
{
i.x = ((0.5 + seed + 32)/128);
swap = tex2D(colorTableSampler,i);
}
if (color.r == 0)
{
i.x = ((0.5 + seed)/128);
swap = tex2D(colorTableSampler, i);
}

return swap;
}

technique ColorSwap
{
pass Pass1
{
// TODO: set renderstates here.

PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

这里是创建纹理的函数。我还应该注意到,纹理生成在没有着色器的情况下也能正常工作,我只得到灰度基础图像。

public static Texture2D createEnemyTexture(GraphicsDevice gd, SpriteBatch sb)
{
//get a random number to pass into the shader.
Random r = new Random();
float seed = (float)r.Next(0, 32);
//create the texture to copy color data into
Texture2D enemyTex = new Texture2D(gd, CHARACTER_SIDE, CHARACTER_SIDE);
//create a render target to draw a character to.
RenderTarget2D rendTarget = new RenderTarget2D(gd, CHARACTER_SIDE, CHARACTER_SIDE,
false, gd.PresentationParameters.BackBufferFormat, DepthFormat.None);
gd.SetRenderTarget(rendTarget);
//set background of new render target to transparent.
//gd.Clear(Microsoft.Xna.Framework.Color.Black);
//start drawing to the new render target
sb.Begin(SpriteSortMode.Immediate, BlendState.Opaque,
SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone);
//send the random value to the shader.
Graphics.GlobalGfx.colorSwapEffect.Parameters["seed"].SetValue(seed);
//send the palette texture to the shader.
Graphics.GlobalGfx.colorSwapEffect.Parameters["colorTable"].SetValue(Graphics.GlobalGfx.palette);
//apply the effect
Graphics.GlobalGfx.colorSwapEffect.CurrentTechnique.Passes[0].Apply();
//draw the texture (now with color!)
sb.Draw(enemyBase, new Microsoft.Xna.Framework.Vector2(0, 0), Microsoft.Xna.Framework.Color.White);
//end drawing
sb.End();
//reset rendertarget
gd.SetRenderTarget(null);
//copy the drawn and colored enemy to a non-volitile texture (instead of render target)
//create the color array the size of the texture.
Color[] cs = new Color[CHARACTER_SIDE * CHARACTER_SIDE];
//get all color data from the render target
rendTarget.GetData<Color>(cs);
//move the color data into the texture.
enemyTex.SetData<Color>(cs);
//return the finished texture.
return enemyTex;
}

为了以防万一,在着色器中加载的代码:

BinaryReader Reader = new BinaryReader(File.Open(@"Content\\shaders\\test.mgfx", FileMode.Open));
colorSwapEffect = new Effect(gd, Reader.ReadBytes((int)Reader.BaseStream.Length));

如果有人有解决此问题的想法,我将不胜感激,如果您需要有关该问题的其他信息,请告诉我。

最佳答案

我不确定为什么在转义反斜杠时在字符串前面有“at”(@) 符号 - 除非你想在字符串中包含\\,但它在文件路径中看起来很奇怪。

您在代码中写道:

BinaryReader Reader = new BinaryReader(File.Open(@"Content\\shaders\\test.mgfx", FileMode.Open));

除非你想在字符串中使用\\

BinaryReader Reader = new BinaryReader(File.Open(@"Content\shaders\test.mgfx", FileMode.Open));

BinaryReader Reader = new BinaryReader(File.Open("Content\\shaders\\test.mgfx", FileMode.Open));

但不要同时使用。

关于c# - Monogame 着色器移植问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com