C# C HLSL/XNA/单对策问题

C# C HLSL/XNA/单对策问题,c#,visual-studio,xna,monogame,C#,Visual Studio,Xna,Monogame,首先,我是着色器和xna的新手 我一直在努力学习本教程: 我已经完成了他说的每一件事,我甚至复制/粘贴了一些部分以完全确定,尽管——它仍然不起作用 采样器s0; 纹理光掩膜; 采样器lightSampler=采样器_状态{ 纹理=; } ; float4像素着色器lightfloat2坐标:TEXCOORD0:COLOR0{ float4 color=tex2Ds0,坐标; float4 lightColor=tex2DlightSampler,坐标; 返回颜色*浅颜色; } 技术1{ 通过P

首先,我是着色器和xna的新手

我一直在努力学习本教程:

我已经完成了他说的每一件事,我甚至复制/粘贴了一些部分以完全确定,尽管——它仍然不起作用

采样器s0; 纹理光掩膜; 采样器lightSampler=采样器_状态{ 纹理=; } ; float4像素着色器lightfloat2坐标:TEXCOORD0:COLOR0{ float4 color=tex2Ds0,坐标; float4 lightColor=tex2DlightSampler,坐标; 返回颜色*浅颜色; } 技术1{ 通过P0{ PixelShader=编译ps_4_0_level_9_1 PixelShaderLight; } } 问题是,当我应用pass 0时,所有内容都变为黑色

我猜lightcolor正在返回零。灯光遮罩是一个渲染目标,我在其中绘制灯光

我真的不知道为什么lightcolor会返回零。如果是这样的话,谁能给我一个暗示,说明我做错了什么

如果你想看的话,这里是我的主要课程:

使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Graphics; 使用Microsoft.Xna.Framework.Input; 命名空间测试游戏 { 公开课测试游戏:游戏 { 图形管理器图形; SpriteBatch SpriteBatch; 位置loc; 公共静态纹理2D光掩膜; 公共静态纹理2D img; 公共静态效应1; RenderTarget2D lightsTarget; RenderTarget2D主目标; 公开测试游戏 { graphics=新的GraphicsDeviceManagerthis; Content.RootDirectory=Content; } 受保护的覆盖无效初始化 { loc=新位置20,20; var pp=GraphicsDevice.PresentationParameters; lightsTarget=新的RenderTarget2D 图形设备,pp.BackBufferWidth,pp.BackBufferHeight; mainTarget=新的RenderTarget2D 图形设备,pp.BackBufferWidth,pp.BackBufferHeight; 初始化; } 受保护的覆盖无效加载内容 { spriteBatch=新spriteBatch图形设备; lightMask=Content.Loadlightmask.png; img=Content.Loadimg.png; effect1=Content.Loadlighteffect; } 受保护的覆盖无效内容 { } 受保护的覆盖无效UpdateGameTime游戏时间 { 如果GamePad.GetStatePlayerIndex.One.Buttons.Back==ButtonState.Pressed | | | Keyboard.GetState.IsKeyDownKeys.Escape 出口 ifloc.Equals新位置21,20 System.Console.WriteLineWorking; base.UpdategameTime; } 受保护的覆盖无效DrawGameTime gameTime { GraphicsDevice.SetRenderTargetlightsTarget; GraphicsDevice.ClearColor.Black; spriteBatch.BeginSpriteSortMode.Immediate、BlendState.Additive; spriteBatch.DrawlightMask,新矢量220,20,彩色。红色; spriteBatch.结束; GraphicsDevice.SetRenderTargetmainTarget; GraphicsDevice.ClearColor.Transparent; spriteBatch.BeginSpriteSortMode.Deferred,BlendState.AlphaBlend; spriteBatch.Drawimg,新矢量250,50; spriteBatch.结束; GraphicsDevice.SetRenderTargetnull; GraphicsDevice.ClearColor.Black; spriteBatch.BeginSpriteSortMode.Immediate、BlendState.AlphaBlend; 效应1.参数[lightMask].SetValuelightsTarget; 效应1.CurrentTechnical.Passes[0]。应用; spriteBatch.DrawmainTarget,矢量2.0,彩色。白色; spriteBatch.结束; 基本游戏时间; } } }
对于像我这样偶然发现这个问题的其他人,您需要使用像素着色器的完整签名,如下所述:

因此,此着色器的正确代码如下所示:

sampler s0;
texture lightMask;
sampler lightSampler=sampler_state {
  Texture=<lightMask>;
};

float4 PixelShaderLight(float4 pos: SV_POSITION, float4 color: COLOR0, float2 coords: TEXCOORD0): SV_TARGET0 {
  float4 color=tex2D(s0, coords);
  float4 lightColor=tex2D(lightSampler, coords);
  return color * lightColor;
}
technique Technique1 {
  pass P0 {
    PixelShader=compile ps_4_0_level_9_1 PixelShaderLight();
  }
}

我也尝试过在monogame中编写着色器,和你一样,当我从教程中复制粘贴着色器显示出积极的结果时,它们仍然什么也不做,或者确实将整个游戏变黑。为什么,我真的说不上来。那个采样器是什么;在你的着色器中?它没有定义,我看不到你从游戏中将参数s0传递到着色器的位置。所以我认为float4 color=tex2Ds0,coords;将返回黑色。我解决了它,我认为s0自动设置为我认为绘制的纹理。我认为问题在于我需要将position:SV_position添加到构造函数中