私のプロジェクトには非常に奇妙な問題があります。頂点と共にインデックス番号を送信して、この番号を HLSL シェーダーで使用しようとしました。
しかし、この数値を 1 のような値に設定すると、シェーダーはその数値から非常に広いスペクトルを取得します - 0 ~ 1 から 1 を超える負の値まで下がります。
(SlimDX で C# を使用しています。これはピクセル シェーダー 2.0 で作成されており、3.0 も試しました。)
int c = 0;
int testValue = 10000;
for (int i = 0; i < tris.Length; i++)
{
vertices[c].Position = tris[i].p0;
vertices[c].Normal = tris[i].normal;
vertices[c].Index = testValue;
vertices[c++].Color = Color.LightBlue.ToArgb();
vertices[c].Position = tris[i].p1;
vertices[c].Normal = tris[i].normal;
vertices[c].Index = testValue;
vertices[c++].Color = Color.LightBlue.ToArgb();
vertices[c].Position = tris[i].p2;
vertices[c].Normal = tris[i].normal;
vertices[c].Index = testValue;
vertices[c++].Color = Color.LightBlue.ToArgb();
}
これが頂点を作成する方法です。インデックス値を除いて、すべてが正常に機能します。色、法線、位置を正しく設定します。
HLSL コードは次のとおりです。
VertexToPixel IndexedRenderedVS( float4 inPos : POSITION, float4 inColor : COLOR0, float3 inNormal : NORMAL, int inIndex : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;
float4x4 xWorldViewProjection = mul(xWorld, xViewProjection);
Output.Position = mul(inPos, xWorldViewProjection);
if (inIndex < 0)
Output.Color = float4(1, 0, 0, 1);
if (inIndex > 0 && inIndex < 1)
Output.Color = float4(0, 1, 0, 1);
if (inIndex > 1)
Output.Color = float4(0, 0, 1, 1);
//Output.Color = inColor;
return Output;
}
PixelToFrame IndexedRenderedPS(VertexToPixel PSIN)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIN.Color;
return Output;
}
そして最後に、私の VertexFormat 構造体:
internal Vector3 Position;
internal int Color;
internal Vector3 Normal;
internal int Index;
internal static VertexElement[] VertexElements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, sizeof(float) * 3, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
new VertexElement(0, sizeof(float) * 7, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
new VertexElement(0, sizeof(float) * 10, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
VertexElement.VertexDeclarationEnd
};
internal static VertexFormat Format
{
get { return VertexFormat.Position | VertexFormat.Diffuse | VertexFormat.Normal | VertexFormat.Texture1; }
}
このコードと非常識なインデックス値 (10000 - 0、1、または負の数を入力しても、同じ画像が得られます。何を入力してもかまいません)。
私はこの写真を手に入れます:
誰が私がどこで間違いを犯したか知っていますか? 値をどこに置き忘れたのかがわかりません。膨大な数の頂点宣言を試み、シェーダー内のすべてを変更しました - そして今、私は正式にアイデアを使い果たしました。
どんな助けでも大歓迎です。ありがとうございました :)