通过HLSL中的像素着色器传递颜色 [英] Passing colors through a pixel shader in HLSL

查看:194
本文介绍了通过HLSL中的像素着色器传递颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像素着色器,应该简单地将输入颜色通过,但是我得到的是恒定的结果.我认为我的语法可能是问题.这是着色器:

I have have a pixel shader that should simply pass the input color through, but instead I am getting a constant result. I think my syntax might be the problem. Here is the shader:

struct PixelShaderInput
{
    float3 color : COLOR;
};

struct PixelShaderOutput
{
    float4 color : SV_TARGET0;
};

PixelShaderOutput main(PixelShaderInput input)
{
    PixelShaderOutput output; 
    output.color.rgba = float4(input.color, 1.0f); // input.color is 0.5, 0.5, 0.5; output is black
    // output.color.rgba = float4(0.5f, 0.5f, 0.5f, 1); // output is gray
    return output;
}

为了进行测试,我在pipleline之前的顶点着色器中传递了0.5、0.5、0.5的COLOR参数.在Visual Studio中逐步遍历像素着色器时,input.color具有正确的值,并且这些值被正确地赋值为output.color.但是,渲染时,使用此着色器的顶点都是黑色的.

For testing, I have the vertex shader that precedes this in the pipleline passing a COLOR parameter of 0.5, 0.5, 0.5. Stepping through the pixel shader in VisualStudio, input.color has the correct values, and these are being assinged to output.color correctly. However when rendered, the vertices that use this shader are all black.

这是顶点着色器元素描述:

Here is the vertex shader element description:

const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
{
    { "POSITION",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR",      0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD",   0, DXGI_FORMAT_R32G32_FLOAT,    0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

我不确定顶点着色器采用RGB输出相同的颜色是否很重要,而像素着色器输出RGBA是否重要. Alpha层至少正常工作.

I'm not sure if it's important that the vertex shader takes colors as RGB outputs the same, but the pixel shader outputs RGBA. The alpha layer is working correctly at least.

如果我注释掉第一个分配(使用input.color),并取消注释另一个分配(带有显式值),则渲染的像素为灰色(如预期).

If I comment out that first assignment, the one using input.color, and uncomment the other assignment, with the explicit values, then the rendered pixels are gray (as expected).

对我在这里做错的任何想法吗?

Any ideas on what I'm doing wrong here?

我正在使用着色器模型4级别9_1,其中禁用了优化并启用了调试信息.

I'm using shader model 4 level 9_1, with optimizations disabled and debug info enabled.

推荐答案

output.color.rgba = float4(input.color, 1.0f);

您的input.color是float4,并且您将其传递给另一个float4,我认为这应该可行

your input.color is a float4 and you are passing it into another float4, i think this should work

output.color.rgba = float4(input.color.rgb, 1.0f);

这就是您将其简单地传递的所有条件

this is all you need to pass it thru simply

 return input.color;

如果您想将颜色更改为红色,请执行类似的操作

if you want to change the colour to red then do something like

input.color = float4(1.0f, 0.0f, 0.0f, 1.0f);
return input.color;

这篇关于通过HLSL中的像素着色器传递颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆