着色器/代码问题

发布于 2024-09-26 04:44:57 字数 2308 浏览 0 评论 0 原文

我有这个基本的 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);

I have this basic 3d application and trying to write my own toon shader, but even if I remove the tooning part it still still just stays plain darkblue when I give a red color to it.

shader code :

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

Custom vertex format :

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

Init of the triangle I am trying to draw :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-10-03 04:44:57

在 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,您可能还想阅读 此博文

In C#, struct fields are ordered sequentially in memory. But the order of fields in your structure does not match what you have set in VertexElements.

It should be:

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

(Not sure if that's the only problem in your code, but that's what stuck out.)

If you are using XNA 4.0, you might also want to have a read of this blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文