Camera XNA到HLSL浮动2的距离向量

Camera XNA到HLSL浮动2的距离向量,camera,xna,monogame,hlsl,pixel-shader,Camera,Xna,Monogame,Hlsl,Pixel Shader,我使用这个像素着色器对纹理应用了噪波效果 float2 noisePower; float noiseFrequency; float camMoveX; float camMoveY; texture noiseTexture; sampler2D noiseSampler = sampler_state { Texture = <noiseTexture>; MipFilter = LINEAR; MinFilter = LINEAR; Mag

我使用这个像素着色器对纹理应用了噪波效果

float2 noisePower;
float noiseFrequency;

float camMoveX;
float camMoveY;

texture noiseTexture;
sampler2D noiseSampler = sampler_state
{
    Texture = <noiseTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

texture water;
sampler2D waterSampler = sampler_state
{
    Texture = <water>;
};


float4 MainPS(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : COLOR
{
    float2 camMove = float2(camMoveX, camMoveY);
    //float4 camMoveTransform = mul(WorldViewProjection, camMove);

    float4 noise = tex2D(noiseSampler, texCoord.xy * noiseFrequency);
    float2 offset = (noisePower * (noise.xy - 0.5f) * 2.0f);

    float4 color = tex2D(waterSampler, texCoord.xy + offset.xy);
    return color;

}

technique oceanRipple
{
    pass P0
    {
        PixelShader = compile PS_SHADERMODEL MainPS();
    }
};
因此,如果相机按矢量(1,0)移动一帧(在世界空间中),我希望通过这个量来抵消噪声采样的位置,以便在相机移动时它采样相同的世界水位置

我很难将这个距离向量转换成HLSL可以使用的东西

由于HLSL将坐标视为在0和1之间进行规范化,因此我尝试在XNA中这样设置camMoveX和Y:

camDisX = (camMove.X / GameOptions.PrefferedBackBufferWidth) 
然后将这些值传递给着色器

不幸的是,这是行不通的。我应该如何将这个距离向量转换成HLSL可以使用的东西?我应该使用顶点着色器和worldViewProj矩阵来解决这个问题吗


这里是一个示例。

不要使用帧之间的摄影机移动距离来计算偏移,而是使用摄影机位置,该位置可以从视图矩阵转置的最后一列中提取,也可以通过累积每帧的摄影机移动来计算

float4 noise = tex2D(noiseSampler, (texCoord.xy + camPos.xy) * noiseFrequency);

直观地说,这更有意义:如果相机(a,b)远离某个原点,则纹理也需要在远离该原点的(a,b)处采样,以使其看起来静止。这与相机移动的速度无关

自从我找到一个解决方案以来,我一直在更新这个。我的结论如下:

    camMove.X = ((_cam.Position.X % GameOptions.PrefferedBackBufferWidth) / (GameOptions.PrefferedBackBufferWidth));
    camMove.Y = ((_cam.Position.Y % GameOptions.PrefferedBackBufferHeight) / (GameOptions.PrefferedBackBufferHeight));
    camMove.X = ((_cam.Position.X % GameOptions.PrefferedBackBufferWidth) / (GameOptions.PrefferedBackBufferWidth));
    camMove.Y = ((_cam.Position.Y % GameOptions.PrefferedBackBufferHeight) / (GameOptions.PrefferedBackBufferHeight));