Use GPUComputationRenderer texture for dynamic InstancedMesh objects positions

Read the texture with heights in vertex shader and add that value to Y-component of translate values of instanceMatrix.

    shader.vertexShader = `
      uniform sampler2D heightMap;
      attribute vec2 instUV;
      varying float vHeight;
      ${shader.vertexShader}
    `.replace(
      `#include <project_vertex>`,
      `
      vec4 mvPosition = vec4( transformed, 1.0 );
      #ifdef USE_INSTANCING
        mat4 im = instanceMatrix;
        float h = texture2D(heightMap, instUV).r; // read
        vHeight = h;
        im[3].y += h * 5.; // add
        mvPosition = im * mvPosition;
      #endif
      mvPosition = modelViewMatrix * mvPosition;
      gl_Position = projectionMatrix * mvPosition;
      `
    );

Demo: https://codepen.io/prisoner849/full/zYRGBmK

PS It’s just an option, not the ultimate solution.

4 Likes