【问题标题】:How to use shader on Texture?如何在纹理上使用着色器?
【发布时间】:2012-08-30 22:14:22
【问题描述】:

我想建立一个基于着色器的光照系统。http://stackoverflow.com/editing-help 为此,我需要将某些功能应用于纹理。 如何通过 C#/XNA 中的着色器算法传递纹理? 我试过这样做:

foreach (EffectPass pass in effect.Techniques[0].Passes)
            {
                pass.Apply();
            }
spriteBatch.Draw(shadowmap,
                new Rectangle(0, 0, range * 2, range * 2),
                new Rectangle((int)screenPos.X - range, (int)screenPos.Y - range, 2 * range, 2 * range),
                Color.White);

但它不起作用。 我也试过这个:

spriteBatch.Begin();

            spriteBatch.Draw(shadowmap,
                new Rectangle(0, 0, range * 2, range * 2),
                new Rectangle((int)screenPos.X - range, (int)screenPos.Y - range, 2 * range, 2 * range),
                Color.White);
            spriteBatch.End();
            graphicsDevice.SetRenderTarget(null);
            effect.Parameters["tex"].SetValue(area);
            foreach (EffectPass pass in effect.Techniques[0].Passes)
            {
                pass.Apply();
            }

但这也无济于事。 我需要通过两次/三次算法传递来传递纹理,所以我需要一种将着色器任意应用于纹理的方法。有没有办法做到这一点?

编辑: HLSL 代码是这样的:

texture tex;
sampler input  : tex;
float red;
float2 mousepos; 
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
    float4 color;
    color=tex2D(input,coords.xy);
    color.r=0.9;
    return color;
}



technique Technique1
{
    pass Pass1
    { 
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }

}

【问题讨论】:

  • 您似乎发布了错误的链接。您可以编辑您的帖子或将其添加为评论吗?

标签: xna hlsl lighting


【解决方案1】:

您可以将Effect 传递给this overload of SpriteBatch.Begin

您也可以在调用SpriteBatch.Draw 之前使用SpriteSortMode.ImmediateApply() 不同的效果。

(在所有其他模式下,SpriteBatch 将批量处理其所有操作 - 设置效果和绘图 - 直到您调用 End。这会提供更好的性能,但不会让您有机会设置新的效果或状态精灵之间。)


如果您直接使用效果(不是通过SpriteBatch),您可以像这样在图形设备上设置纹理:

GraphicsDevice.Texture[0] = myTexture;

还有Apply() 您的效果,设置任何其他图形设备状态,然后通过调用GraphicsDevice.Draw*() 方法之一来跟踪它。


也要注意foreach 循环中的错误。您必须在每次调用Apply() 后绘制一些东西(即:在循环内)。如果将效果传递给SpriteBatch.Begin,则不需要循环,因为SpriteBatch 会在内部为您处理。

【讨论】:

  • 我已经尝试过您的解决方案,但它仍然不起作用。我无法将其设置为纹理,因为它是渲染目标。我需要做的是基本上采用该纹理然后对其进行转换。直到在其上运行两个算法之后才绘制它。我将尝试使用两个 RenderTargets 并将输出从一个发送到另一个。
  • RenderTarget2DTexture2D。你可以这样使用它。唯一的附带条件是,当它被设置为渲染目标时,您不能将其用作纹理。在将其用作纹理之前使用SetRenderTarget(null) - 看起来你已经在这样做了,不过?正如您所建议的那样,为多通道效果使用多个目标是正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多