Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单游戏2D照明_C#_Monogame_Hlsl_Effects_Lighting - Fatal编程技术网

C# 单游戏2D照明

C# 单游戏2D照明,c#,monogame,hlsl,effects,lighting,C#,Monogame,Hlsl,Effects,Lighting,我一直在尝试在Monogame中实现一些非常基本的照明,下面是我在网上找到的几个教程(即,这里): 我曾试图模仿他们的做法,但没有成功,希望有人能告诉我原因 我加载一个灯光遮罩纹理(与第一个链接中的纹理相同),一个简单的对象来测试灯光和我的效果文件(这些已经在类的后面声明): 在这里画所有的东西: GraphicsDevice graphicsDevice = ScreenManager.Instance.GraphicsDeviceManager.GraphicsDevice; grap

我一直在尝试在Monogame中实现一些非常基本的照明,下面是我在网上找到的几个教程(即,这里):

我曾试图模仿他们的做法,但没有成功,希望有人能告诉我原因

我加载一个灯光遮罩纹理(与第一个链接中的纹理相同),一个简单的对象来测试灯光和我的效果文件(这些已经在类的后面声明):

在这里画所有的东西:

GraphicsDevice graphicsDevice = ScreenManager.Instance.GraphicsDeviceManager.GraphicsDevice;

graphicsDevice.SetRenderTarget(lightsTarget);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
spriteBatch.Draw(lightMask, ScreenCentre - new Vector2(128), Color.White);
spriteBatch.End();

graphicsDevice.SetRenderTarget(mainTarget);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(cursor, ScreenCentre - new Vector2(64), Color.White);
spriteBatch.End();

graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Black);

effect.Parameters["LightMask"].SetValue(lightsTarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
spriteBatch.End();

(新矢量2(128)和新矢量(64)仅位于屏幕中部的精灵)。

这是我的效果文件:

#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

sampler2D TextureSampler : register(s0);

Texture2D LightMask;
sampler2D LightMaskSampler = sampler_state { Texture = <LightMask>; };

float4 MainPS(float4 position : SV_POSITION, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
  float4 lightColour = tex2D(LightMaskSampler, texCoord);
  float4 tex = tex2D(TextureSampler, texCoord);

  return lightColour * tex;
}

technique SpriteDrawing
{
    pass Pass1
    {
        PixelShader = compile ps_5_0 MainPS();
    }
};
#如果是OPENGL
#定义SV_位置
#定义VS_着色器模型VS_3_0
#定义PS_着色器模型PS_3_0
#否则
#定义VS_SHADERMODEL VS_4_0_level_9_1
#定义PS_着色器模型PS_4_0_级别9_1
#恩迪夫
采样器2D纹理采样器:寄存器(s0);
纹理2D光掩膜;
sampler2D LightMasksSampler=采样器_状态{纹理=;};
float4 MainPS(float4位置:SV_位置,float4颜色:COLOR0,float2 texCoord:TEXCOORD0):COLOR0
{
float4 lightcolor=tex2D(LightMaskSampler,texCoord);
float4 tex=tex2D(TextureSampler,texCoord);
返回浅色*tex;
}
喷绘技术
{
通行证1
{
PixelShader=编译ps_5_0 MainPS();
}
};
代码有点零碎,因为我只是想让它工作。据我所知,我已经正确地设置了一切,但我看到的只是一个黑屏

当我在没有效果的情况下单独绘制每个渲染状态时,我确实在正确的位置看到了每个纹理,因此我的着色器似乎没有正确地将它们组合在一起


如果能对此事有所了解(廉价笑话),我们将不胜感激。

简单检查一下:当你写“light mask texture(与第一个链接中的相同)”时,你是否真的从该页面下载了
lightmask.png
,或者创建/下载了半透明的白色面具?因为文章作者写了一篇他在演示中使用的是白色透明渐变的文章,在他的白色网站背景下有一个可以看到的黑色层。我试着运行这个(用),它成功了。但是,如果
main目标
完全为黑色,则不会看到太多内容,因为着色器仅影响alpha通道(给定正确的纹理)。尝试用
图形设备清除
main目标
。清除(颜色.绿色)或类似的东西,看看会发生什么。
GraphicsDevice graphicsDevice = ScreenManager.Instance.GraphicsDeviceManager.GraphicsDevice;

graphicsDevice.SetRenderTarget(lightsTarget);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
spriteBatch.Draw(lightMask, ScreenCentre - new Vector2(128), Color.White);
spriteBatch.End();

graphicsDevice.SetRenderTarget(mainTarget);
graphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(cursor, ScreenCentre - new Vector2(64), Color.White);
spriteBatch.End();

graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Black);

effect.Parameters["LightMask"].SetValue(lightsTarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
spriteBatch.End();
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

sampler2D TextureSampler : register(s0);

Texture2D LightMask;
sampler2D LightMaskSampler = sampler_state { Texture = <LightMask>; };

float4 MainPS(float4 position : SV_POSITION, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
  float4 lightColour = tex2D(LightMaskSampler, texCoord);
  float4 tex = tex2D(TextureSampler, texCoord);

  return lightColour * tex;
}

technique SpriteDrawing
{
    pass Pass1
    {
        PixelShader = compile ps_5_0 MainPS();
    }
};