tangram-es icon indicating copy to clipboard operation
tangram-es copied to clipboard

Passing a feature.value to a shader uniform

Open mkandulavm opened this issue 2 years ago • 3 comments

Hi

This might be reasonably obvious, but I need to pass some vector tile attributes (lets say feature.value which is a float) into the shader , as a uniform.

How can I do this ? If I set a shader uniform and set it like this, I am getting error.

shaders:
      uniforms:
        u_value: feature.value

Thank you for the help !!

mkandulavm avatar Apr 02 '22 07:04 mkandulavm

This isn't possible with the current OpenGL pipeline in Tangram ES. As the name "uniform" implies, it is a value that remains the same for every element in the draw call. For efficiency, Tangram combines many features in a single draw call. This means that a shader can't use different uniform values for different features.

matteblair avatar Apr 02 '22 22:04 matteblair

So I guess it is a documentation issue, I opened an issue in the other repo. Hope it is still being maintianed.

westnordost avatar Apr 08 '22 15:04 westnordost

@mkandulavm though, there is a hack to workaround this. The color is available in the shader and the color can be set via javascript, i.e. can be set based on some feature's value. The color is a vec4 in the shader.

Now let's say you don't need for example the alpha channel of the color because you want it to draw always opaque anyway. Then, you could encode the attribute into the 8 bits of the alpha channel and in the shader, extract this information again. This approach can be taken even more to the extreme when you for example only need the hue for the color but the saturation + lightning is always the same. Then you got 3x8 = 24bits at your disposal to fill with data otherwise.

🤪

westnordost avatar Apr 15 '22 12:04 westnordost