Entity Black
Entity Black

Reputation: 3491

Texture lookup vs handle attribute in shaders

My question is related to generating height maps with noise functions and others.

WebGL offers two interesting functions: gl.texImage2D and gl.readPixels. I would say they are kind of similar, first one uploads image data from canvas to the currently bound WebGLTexture, second fills Uint8Array buffer with the pixel data from canvas.

Also as I understand, gl.texImage2D is almost identical as gl.readPixels if accepting different params.

I have seen various (WebGL) height map implementations and as I remember all of them uses texImage2D and noone uses readPixels.

Lets say we have a plane made of vertices with [x, z] coordinates only and we are looking for y coordinate to distort our plane. So we can find the [y] coordinate via texture lookup and so we bind the texture to our program OR we could bind another buffer with data from canvas and read [y] as attribute.

After thinking how texture lookup works and how attributes work, texture lookup must be much more heavy compared to attribute? But how much?

<--- this is my primary question related to how texture lookup works

But then I don't understand why people use textures so much?

<--- In case first one is true, secondary question is about gl.texImage2D and gl.readPixels, how does it work from the point of javascript. Have you seen any demo with readPixels? Is there any advantage I don't see?

Upvotes: 1

Views: 177

Answers (1)

Winchestro
Winchestro

Reputation: 2188

the difference between texImage2D / texSubImage2D and readPixels is that they do the exact opposite thing. One sends data to the gpu, the other reads that data back, which is (only) useful if you need that data in javascript.

As for the reason why people use textures for heightmaps instead of attributes is because it's easier to modify. It's a convenience, not a rule. If you want, you can implement it with attributes. Rendering it will most likely be slightly faster, modifying it will be much slower.

If you just want to load the terrain once, and never touch the data again, use attributes. If you want to modify it a lot, use textures.

I hope this answer is clearer. Didn't realize that it doesn't make that much sense to even attempt to explain the difference between textures and attributes to answer it.

Upvotes: 1

Related Questions