【问题标题】:How to calculate distance for fog effect model on xna?如何计算 xna 上雾效模型的距离?
【发布时间】:2013-03-14 18:49:39
【问题描述】:

我为在我的 xna 游戏中添加 效果而苦苦挣扎了一段时间。 我在文件 (.Fx) 中使用自定义着色器效果。 “PixelShaderFunction”可以正常工作。但问题是我所有的土地都以相同的方式着色。 我认为问题出在相机和模型之间的距离的计算上。

float distance = length(input.TextureCoordinate - cameraPos);

这是我使用“PixelShaderFunction”的完整代码

// Both techniques share this same pixel shader.
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
      float distance = length(input.TextureCoordinate - cameraPos);
      float l = saturate((distance-fogNear)/(fogFar-fogNear));
      return tex2D(Sampler, input.TextureCoordinate) * lerp(input.Color, fogColor, l);
}

【问题讨论】:

    标签: xna shader game-engine effects fog


    【解决方案1】:

    如果您的 input.TextureCoordinate 确实代表采样器的纹理坐标,那么您尝试计算距离的方式是错误的。

    您可以按如下方式更改 PixelShaderFunction 的主体:

    float distance = distance(cameraPos, input.Position3D);
    float l = saturate((distance-fogNear)/(fogFar-fogNear));
    return lerp(tex2D(Sampler, input.TextureCoordinate), fogColor, l);
    

    将以下内容添加到您的 VertexShaderOutput 声明中:

    float4 Position3D : TEXCOORD1;
    

    在您的顶点着色器中,使用与世界矩阵相乘的顶点位置填充 Position3D:

    output.Position3D = mul(input.pos, matWorld);
    

    【讨论】:

    • 非常感谢!你的例子非常适合我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2017-08-10
    • 2014-08-21
    • 2017-08-21
    相关资源
    最近更新 更多