2010-10-06 74 views
1

我有这个基本的3D应用程序,并试图写我自己的香椿着色器,但即使我删除了香椿的部分,它仍然只是保持普通darkblue,当我给它一个红色。有着色器/代码的问题

shader代码:

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); 

回答

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,你可能还需要阅读this blog post

+0

工作感谢要去做那个 – Xyro 2010-10-06 16:22:51