【问题标题】:Set shader's texture filter from C# in MonoGame在 MonoGame 中从 C# 设置着色器的纹理过滤器
【发布时间】:2016-11-26 16:58:12
【问题描述】:

假设我的着色器中有这两个变量:

HLSL

texture ModelTexture;
sampler2D TextureSampler

这就是我设置纹理的方式:

C#

myEffect.Parameters["ModelTexture"].SetValue(woodTexture);

采样器有什么作用?没有EffectParameter.SetValue(SamplerState) 重载,但我想在运行时更改过滤器。我该怎么办?

我很确定其余代码没问题,因为如果我直接在 HLSL 中初始化 TextureSampler,它会正确地绘制带纹理的对象。

编辑:

我尝试在 GraphicsDevice 中设置一个 SamplerState 对象。

C#

GraphicsDevice.SamplerStates[0] = new SamplerState { Filter = TextureFilter.Linear };

HLSL

sampler2D TextureSampler : register(s0);

现在纹理分配(参见第二个 sn-p)抛出 NullReferenceException

我也尝试过像上面那样设置纹理,而不是通过EffectParameter

C#

GraphicsDevice.Textures[0] = woodTexture;

HLSL

texture ModelTexture : register(t0);

程序编译运行,但未加载纹理(渲染对象为黑色)。

【问题讨论】:

    标签: c# xna shader monogame hlsl


    【解决方案1】:

    很久没用过XNA框架了,在谷歌简单搜索一下就得到了这样的结果:

    What Is Sampler State?

    采样器状态决定了如何使用纹理寻址模式、过滤和细节级别对纹理数据进行采样。使用 SamplerState 类创建采样器状态对象。使用 GraphicsDevice.SamplerStates 属性将采样器状态设置为图形设备。

    GraphicsDevice.SamplerStates Property

    此方法返回为 GraphicsDevice 设置的最后一个采样器状态,或默认的 GraphicsDevice 采样器状态(如果之前未设置)。可编程着色器使用采样器编号引用纹理,该编号设置为 GraphicsDevice.Textures 的索引。

    因此,您必须创建一个 SamplerState 对象并将其设置在 GraphicsDevice.SamplerStates 插槽之一中。

    【讨论】:

    • @matt-pielat 使用GraphicsDevice.Textures 设置您的纹理,并在您的着色器文件中使用sampler texture : register(s0); 引用它,其中采样器索引对应于纹理集合中的索引。请参阅此link 中的示例
    • 编译并运行,但渲染的模型是黑色的。
    【解决方案2】:

    采样器状态设置在图形设备对象上或着色器本身内部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2020-09-06
      • 2013-09-17
      • 2012-12-27
      相关资源
      最近更新 更多