HLSL DirectX:PS 中生成的颜色存在问题

问题描述 投票:0回答:1

下面的简单 HLSL 代码生成 3 行,其中包含表中的颜色,由 SV_VertexID 索引。 预计每条线的顶点颜色不同,中间颜色不同。

“选项2”可以:获取VS(查找表)中的颜色并将其发送到PS。

“选项1”不行:将索引发送到PS并直接在PS中获取颜色(通过查找表)。 在这种情况下,每条线具有相同的颜色(不改变颜色)。

我不明白为什么“选项 1”不起作用。

这是着色器代码:

static float2 vertices[6] = {
   float2(-0.95f, -0.95f),
   float2(0.95f, 0.95f),
   float2(-0.95f, 0),
   float2(0.95f, 0),
   float2(-0.95f, 0.95f),
   float2(0.95f, -0.95f)
};

static float3 colors[6] = {
   float3(1,0,1), // purple
   float3(0,0,1), // blue
   float3(0,1,1), // cyan (azul claro)
   float3(0,1,0), // green
   float3(1,1,0), // yellow
   float3(1,0,0) // red
};

struct Output { 
  float4 pos : SV_POSITION;
  float3 color1 : COLOR;
  uint vID : TEXCOORD0; 
};

//----------------------------------------- Vertex Shader
Output VSMain(uint vID : SV_VertexID) {
   Output v1;
   v1.pos = float4(vertices[vID], 0, 1);
   v1.color1 = colors[vID];
   v1.vID = vID;
   return v1;
}

//----------------------------------------- Pixel Shader
float4 PSMain(Output v1) : SV_Target {

   float4 color2 = float4(colors[v1.vID],0); // option 1
   //float4 color2 = float4(v1.color1,0); // option 2

   return color2;
}
directx hlsl
1个回答
0
投票

Maico De Blasio 给了我一个方向,所以我应该回答我自己的问题。

“选项1”应该是:将插值索引发送到PS(v1.vID应该是浮点数)并尝试直接从查找表中获取颜色,这没有任何意义,因为“索引”是高度碎片化的,而不是整数。

或者,可以在像素着色器中构建基于此“索引”的颜色,如下面的代码所示:

static float2 vertices[6] = {
   float2(-0.95f, -0.95f),
   float2(0.95f, 0.95f),
   float2(-0.95f, 0),
   float2(0.95f, 0),
   float2(-0.95f, 0.95f),
   float2(0.95f, -0.95f)
};

struct Output { 
  float4 pos : SV_POSITION;
  float vID : none; 
};

//----------------------------------------- Vertex Shader
// executed once for each vertex of the element, so this can be 'heavy'.
//
Output VSMain(uint vID : SV_VertexID) {
   Output v1;
   v1.pos = float4(vertices[vID], 0, 1);
   v1.vID = (float)vID;
   return v1;
}

//----------------------------------------- Pixel Shader
// executed once for each pixel of the element, so this should be 'light'.
// for each pixel, v1 is INVENTED (interpolated) between element vertices.
//
float4 PSMain(Output v1) : SV_Target {

   int intPart = (int)v1.vID; 
   float decPart = 1 + intPart - v1.vID;

   switch (intPart)
   {
       case 0: return float4(decPart,0,0,0);
       case 2: return float4(0,decPart,0,0);
       case 4: return float4(0,0,decPart,0);
   }
   return float4(0,0,0,0);
}
© www.soinside.com 2019 - 2024. All rights reserved.