title: webgl glsl emulate texture3d
body: <p>I'm porting a piece of opengl to webgl and i'm trying to emulate texture3d. Somehow something is going wrong.</p>
  
  <p>No interpolation is needed because it is only used for calculations. I'm not sure about the geometry part of the original code,  the per layer properties are now fetched trough a texture.</p>
  
  <p><strong>Update</strong> : Ok i rewrote the texture 3d functions, I still encounter some problems : </p>
  
  <pre><code>const vec3 volumeTextureSize = vec3( 256.0, 128.0, 32.0 );
  const vec2 imageSize = vec2( 1024.0, 1024.0 );
  
  vec2 uvFromUvw( vec3 uvw ) {
      const vec3 size = volumeTextureSize;
      const vec2 layersPerDim = size.xy / imageSize.xy;
      const vec2 pixelsPerLayer = imageSize / layersPerDim;
  
      // normalized in
      float layer = floor(uvw.z * size.z);
  
      float layerX = mod(layer, layersPerDim.x);
      float layerY = floor(layer / layersPerDim.x);
  
      vec2 layerUv = vec2(layerX, layerY) * pixelsPerLayer;
      vec2 layerSpaceUv = (uvw.xy * size.xy) / layersPerDim;
  
      vec2 uv = layerSpaceUv + layerUv;
  
      uv /= imageSize;
  
      return uv;
  }
  
  vec4 texture3D( sampler2D tex, vec3 uvw ) {
      vec2 uv = uvFromUvw( uvw );
      return texture2D(tex, uv);
  }
  
  vec3 uvwFromUv( vec2 uv ) {
      const vec3 size = volumeTextureSize;
      const vec2 layersPerDim =  imageSize.xy / size.xy;
      const vec2 pixelsPerLayer = imageSize / layersPerDim;
  
      vec2 normUv = uv / imageSize;
  
      vec2 layerUv = floor(vec2(normUv * layersPerDim.xy));
  
      vec3 uvw = vec3(0.0);
  
      uvw.z = floor(layerUv.x + floor( layerUv.y * layersPerDim.x));
      uvw.xy = uv - (layerUv * pixelsPerLayer.xy);
  
      // normalized coords
      // uvw.xy /= size.xy;
      uvw.z /= layersPerDim.x * layersPerDim.y;
  
      return uvw;
  }
  </code></pre>
  
  <p>this is the original code i want to port:</p>
  
  <pre><code>glActiveTexture(GL_TEXTURE0 + deltaJUnit);
  glGenTextures(1, &amp;deltaJTexture);
  glBindTexture(GL_TEXTURE_3D, deltaJTexture);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, RES_MU_S * RES_NU, RES_MU, RES_R, 0, GL_RGB, GL_FLOAT, NULL);
  
  
  
      glFramebufferTextureEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, deltaJTexture, 0);
  
      for (int layer = 0; layer &lt; RES_R; ++layer) {
          setLayer(jProg, layer);
          .. also set uniforms
          drawQuad();
      }
  </code></pre>
  
  <p>and the glsl</p>
  
  <pre><code>#ifdef _VERTEX_
  
  void main() {
      gl_Position = gl_Vertex;
  }
  
  #endif
  
  #ifdef _GEOMETRY_
  #extension GL_EXT_geometry_shader4 : enable
  
  void main() {
      gl_Position = gl_PositionIn[0];
      gl_Layer = layer;
      EmitVertex();
      gl_Position = gl_PositionIn[1];
      gl_Layer = layer;
      EmitVertex();
      gl_Position = gl_PositionIn[2];
      gl_Layer = layer;
      EmitVertex();
      EndPrimitive();
  }
  
  #endif
  </code></pre>