heightmap sample in vertex shader

Started by
6 comments, last by widmowyfox 8 years, 1 month ago

It's strange. I have


vout.PosW.y = gHeightMap.SampleLevel( samHeightmap, float3(x,z,0.0f), 0 ).r;

height map is 4096x4096 grayscale image.

What numbers should be in x,z-range?

I try with

PosW.x/4096

PosW.z/4096

when posw <4096 and posw>0 I always get same result, posw is random number. I tried test with vertexID and InstanceID, no matter what I write to x,z I always have same result. I tried with other textures but without any effect.

Advertisement

They should be in the range [0, 1].

Be sure you're sampling from the center of the texel (especially important for unfiltered texture reads like in a vertex shader). If you're sampling from the border between pixels, you may get any of the 4 neighboring values (not exactly "random", but it might look like it).

What does your grayscale image look like?

What does your rendered grid look like?

Whats your RNG look like? You may not be getting the random you think you are. Have you done some debugging at all?

But when i do this for exemple:


vout.PosW.y = gHeightMap.SampleLevel( samHeightmap, float3(vin.InstanceId/100,vin.InstanceId/100,0.0f), 0 ).r;

I still have same result, it is impossible coz InstanceId. Any suggestions?

Edit:

I do this


if(vin.VertexId==1)
vout.PosW.y = gHeightMap.SampleLevel( samHeightmap, float3(0.2f,0.2f,0.0f), 0 ).r;
else if(vin.VertexId==2)

vout.PosW.y = gHeightMap.SampleLevel( samHeightmap, float3(0.7f,0.7f,0.0f), 0 ).r;

Same result on VertexId 1 and 2, always 0 at output

Here's a snippet of a shader I used for something, it calculates UV=(0,0)-(1,1) based on SV_VertexID, and the amount of vertices it expects is cDimensionsI.x * cDimensionsI.y (cDimensionsF is the same values, expressed as floats):

Note the % and /, and the casting to floats etc.:


// Input declaration
struct VS_INPUT
{
	uint VertexID : SV_VertexID;
};

// Const buffer
cbuffer GlobalData : register(b0)
{
	uint4 cDimensionsI;
        float4 cDimensionsF;
};

// In main shader:

float2 uv = float2(float(In.VertexID % cDimensionsI.x) / cDimensionsF.x, float(In.VertexID / cDimensionsI.x) / cDimensionsF.y);

.:vinterberg:.

vin.InstanceId/100

Is that integer division?

Sounds like your texture isn't correctly loaded or bound, and your SampleLevel always returns 0.

0 is the default that will be returned if no texture is available to read from.

If you run your game from Visual Studio with the debugger and create your device with D3D11_CREATE_DEVICE_DEBUG you should get debug output in the Output window in Visual Studio if that is the case.

Problem solved, the result was very close to 0 but after multiply it can be used. Thanks for answers.

This topic is closed to new replies.

Advertisement