使用顶点着色器的WebGL高度图,使用32位而不是8位

时间:2015-02-21 23:34:48

标签: opengl-es webgl vertex-shader heightmap

我正在使用以下顶点着色器(礼貌http://stemkoski.github.io/Three.js/Shader-Heightmap-Textures.html)从灰度高度图生成地形:

uniform sampler2D bumpTexture;
uniform float bumpScale;

varying float vAmount;
varying vec2 vUV;

void main()
{
  vUV = uv;
  vec4 bumpData = texture2D( bumpTexture, uv );

  vAmount = bumpData.r; // assuming map is grayscale it doesn't matter if you use r, g, or b.

  // move the position along the normal
  vec3 newPosition = position + normal * bumpScale * vAmount;

  gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0);
}

我希望有32位分辨率,并生成一个高度图,将高度编码为RGBA。我不知道如何更改着色器代码以适应这一点。任何方向或帮助?

2 个答案:

答案 0 :(得分:3)

bumpData.r.g.b.a都是[0.0,1.0]范围内的数量,相当于原始字节值除以255.0

因此,根据您的字节顺序,原始转换回原始int可能是:

(bumpData.r * 255.0) + 
(bumpdata.g * 255.0 * 256.0) + 
(bumpData.b * 255.0 * 256.0 * 256.0) + 
(bumpData.a * 255.0 * 256.0 * 256.0 * 256.0)

这与带有向量(255.0, 65280.0, 16711680.0, 4278190080.0)的点积相同,这可能是实现它的更有效的方法。

答案 1 :(得分:0)

使用threejs

const generateHeightTexture = (width) => {
  // let max_texture_width = RENDERER.capabilities.maxTextureSize;
  let pixels = new Float32Array(width * width)
  pixels.fill(0, 0, pixels.length);
  let texture = new THREE.DataTexture(pixels, width, width, THREE.AlphaFormat, THREE.FloatType);

  texture.magFilter = THREE.LinearFilter;
  texture.minFilter = THREE.NearestFilter;
  // texture.anisotropy = RENDERER.capabilities.getMaxAnisotropy();
  texture.needsUpdate = true;
  console.log('Built Physical Texture:', width, 'x', width)
  return texture;
}