texture3d-from-2d-nearest.glsl.ts 665 B

12345678910111213141516
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Michael Krone <michael.krone@uni-tuebingen.de>
  6. */
  7. export default `
  8. vec4 texture3dFrom2dNearest(sampler2D tex, vec3 pos, vec3 gridDim, vec2 texDim) {
  9. float zSlice = floor(pos.z * gridDim.z + 0.5); // round to nearest z-slice
  10. float column = intMod(zSlice * gridDim.x, texDim.x) / gridDim.x;
  11. float row = floor(intDiv(zSlice * gridDim.x, texDim.x));
  12. vec2 coord = (vec2(column * gridDim.x, row * gridDim.y) + (pos.xy * gridDim.xy)) / texDim;
  13. return texture2D(tex, coord);
  14. }
  15. `;