相比于通过改变imageData的方式,本次将透明度阶梯图和温度调色板图传入着色器,在着色器中进行纹理采样最终生成热力图。
着色器代码很简单,先对透明度阶梯图采样,再根据采样值对温度调色板图采样得到最终片元颜色。
<script type="x-shader/x-vertex" id="vertexshader">
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vUv;
uniform sampler2D alphaScaleMap;
uniform sampler2D paletteMap;
void main() {
vec4 alphaColor = texture2D(alphaScaleMap, vUv);
vec4 color = texture2D(paletteMap, vec2(alphaColor.a, 0.0));
gl_FragColor = vec4(color.r, color.g, color.b, alphaColor.a);
}
</script>
案例源代码:https://github.com/NCQuan/heatMap_Shader/blob/main/heatMap_shader.html