1

私はこの基本的な 3D アプリケーションを持っていて、独自のトゥーン シェーダーを作成しようとしていますが、トゥーン部分を削除しても、赤色を与えると、まだ濃い青色のままです。

シェーダーコード:

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
    float4 Color : COLOR0;

};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float LightAmount : TEXCOORD1;
    float4 Color : COLOR0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    output.Color = input.Color;
    float3 worldNormal = mul(input.Normal, World);
    output.LightAmount = dot(worldNormal, LightDirection);

    // TODO: add your vertex shader code here.

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    float4 color = input.Color;

    float light;

    if (input.LightAmount > ToonThresholds[0])
        light = ToonBrightnessLevels[0];
    else if (input.LightAmount > ToonThresholds[1])
        light = ToonBrightnessLevels[1];
    else
        light = ToonBrightnessLevels[2];

    color.rgb *= light;

    return color; 
}

カスタム頂点フォーマット:

public struct VertexPositionNormalColored
{
    public Vector3 Position;
    public Color Color;
    public Vector3 Normal;

    public static int SizeInBytes = 7 * 4;
    public static VertexElement[] VertexElements = new VertexElement[]
    {
        new VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
        new VertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
        new VertexElement(24,VertexElementFormat.Color,VertexElementUsage.Color,0)
    };
}

私が描こうとしている三角形の初期化:

  testVetices = new VertexPositionNormalColored[3];
  testVetices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
  testVetices[0].Color = Color.Red;
  testVetices[0].Normal = new Vector3(0, 0, 1);
  testVetices[1].Position = new Vector3(0, 0.5f, 0f);
  testVetices[1].Color = Color.Red;
  testVetices[1].Normal = new Vector3(0, 0, 1);
  testVetices[2].Position = new Vector3(0.5f, -0.5f, 0f);
  testVetices[2].Color = Color.Red;
  testVetices[2].Normal = new Vector3(0, 0, 1);
4

1 に答える 1

1

C# では、structフィールドはメモリ内で順番に並べられます。しかし、構造内のフィールドの順序が で設定した順序と一致しませんVertexElements

そのはず:

public struct VertexPositionNormalColored
{
    public Vector3 Position;
    public Vector3 Normal;
    public Color Color; // oh look I've moved!

    public static int SizeInBytes = 7 * 4;
    public static VertexElement[] VertexElements = new VertexElement[]
    {
        new VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
        new VertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
        new VertexElement(24,VertexElementFormat.Color,VertexElementUsage.Color,0)
    };
}

(それがあなたのコードの唯一の問題かどうかはわかりませんが、それが突き出たものです。)

XNA 4.0 を使用している場合は、このブログ投稿もお読みください。

于 2010-10-06T15:33:37.710 に答える