简体   繁体   English

Directx:基于HLSL纹理的高度图。 (着色器5.0)

[英]Directx: HLSL Texture based height map. (Shader 5.0)

I'm trying to implement a GPU based height map the simplest (and fastest) way that I know how. 我正在尝试以我知道的最简单(最快)的方式来实现基于GPU的高度图。 I passed a (.png, D3DX11CreateShaderResourceViewFromFile() ) texture into the shader, and I'm attempting to sample it for the current pixel value. 我将(.png, D3DX11CreateShaderResourceViewFromFile() )纹理传递到着色器中,并且尝试将其采样为当前像素值。 Seeing a float4 , I'm currently assigning a color value from a channel to offset the y value. 看到float4 ,我当前正在从通道分配颜色值以补偿y值。

Texture2D colorMap : register(t0);
SamplerState colorSampler : register(s0);

...

VOut VShader(float4 position : POSITION, float2 Texture : TEXCOORD0, float3 Normal : NORMAL)
{
    VOut output;

    float4 colors = colorMap.SampleLevel(colorSampler, float4(position.x*0.001, position.z*0.001, 0,0 ),0);
    position.y = colors.x*128;

    output.position = mul(position, WVP);
    output.texture0 = Texture;
    output.normal = Normal;

    return output;
}

The texture is imported correctly, and I've inserted another texture and was able to successfully blend the texture with another texture (through multiplication of values), so I know that the float4 struct contains values of a sort capable of having arithmetic performed on it. 纹理已正确导入,并且我插入了另一个纹理,并能够成功地将纹理与另一个纹理混合(通过值的乘积),因此我知道float4结构包含能够对其执行算术运算的值。

In the Vertex function, attempting to extract the values yields nothing on a grid: 在顶点函数中,尝试提取值在网格上不会产生任何结果: 在此处输入图片说明

The concept seemed simple enough on paper... 这个概念在纸面上似乎很简单。

Since you're using a Texture2D, the Location parameter needs to be a float2. 由于您使用的是Texture2D,因此Location参数必须为float2。

Also, make sure that location goes from (0,0) to (1,1). 此外,请确保位置从(0,0)变为(1,1)。 For your mapping to be correct, the grid would need to be placed from (0,0,0) to (1000,0,1000). 为了使映射正确,网格需要放置在(0,0,0)到(1000,0,1000)之间。

If this is the case then this should work: 如果是这种情况,那么它应该起作用:

SampleLevel(colorSampler, position.xz*0.001 ,0);

Edit 编辑

I'm curious as to how you're testing this. 我很好奇您如何对此进行测试。 I tried compiling your code, with added definitions for VOut and WVP, and it fails. 我尝试编译您的代码,并添加了VOut和WVP的定义,但失败了。 One of the errors is that location parameter which is a float4 and should be a float2. 错误之一是该位置参数为float4,应为float2。 The other error I get is the name of the function; 我得到的另一个错误是函数的名称。 it should be main. 它应该是主要的。

If you happen to be using Visual Studio, I strongly recommend using the Graphics Debugging tools and check all the variables. 如果您恰好使用Visual Studio,我强烈建议您使用图形调试工具并检查所有变量。 I suspect the colorMap texture might be bound to the pixel shader but not the vertex shader. 我怀疑colorMap纹理可能绑定到像素着色器,但不绑定到顶点着色器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM