【问题标题】:What is relation between type and format of texture纹理的类型和格式之间有什么关系
【发布时间】:2020-05-26 12:47:44
【问题描述】:

我正在使用 three.js 进行体积渲染原始数据。我很难理解three.js中纹理的类型和格式(在我的例子中是texture3d)之间的关系。

可用的纹理类型有:

可用格式为:

我尝试为原始数据的 u8int 数据类型设置类型为 THREE.UnsignedIntType,但它给出了无效的内部格式纹理错误,但对于 THREE.UnsignedByteType 类型,一切正常.

这是我的纹理代码,

    var texture = new THREE.DataTexture3D(data, dims[0], dims[1], dims[2]);
    texture.format = THREE.RedFormat;
    texture.type = THREE.UnsignedByteType;

谁能说说两者之间的关系。由于原始数据类型可以是任何位 (8/16/32) 和类型 (int/float),因此我无法设为静态。

【问题讨论】:

  • 你把THREE.UnsignedIntType放到哪里了?
  • @BDL 我把它放在纹理中,我在添加代码的地方编辑了我的问题。 dims 是三个方向的原始数据长度数组。我把 UnSignedIntType 放在上面代码的第三行,错误带有无效的 internalformat。根据three.js 文档,库本身从纹理的类型和格式中获取 internalformat 属性。不过也可以手动分配。

标签: three.js webgl


【解决方案1】:

three.js 纹理格式和类型是 WebGL 格式和类型的别名。只允许某些组合。

WebGL1 的有效组合列表是

| format          | type
+-----------------+------
| RGBA            | UNSIGNED_BYTE
| RGB             | UNSIGNED_BYTE
| RGBA            | UNSIGNED_SHORT_4_4_4_4
| RGBA            | UNSIGNED_SHORT_5_5_5_1
| RGB             | UNSIGNED_SHORT_5_6_5
| LUMINANCE_ALPHA | UNSIGNED_BYTE
| LUMINANCE       | UNSIGNED_BYTE
| ALPHA           | UNSIGNED_BYTE

所以把它翻译成three.js是

| format               | type
+----------------------+------
| RGBAFormat           | UnsignedByteType
| RGBFormat            | UnsignedByteType
| RGBAFormat           | UnsignedShort4444
| RGBAFormat           | UnsignedShort5551
| RGBFormat            | UnsignedShort565
| LuminanceAlphaFormat | UnsignedByteType
| LuminanceFormat      | UnsignedByteType
| AlphaFormat          | UnsignedByteType

注意:WebGL1 不直接支持 3D 纹理,尽管您可以通过创意着色器自己实现它们

WebGL2 allows a much larger list of combinations

请注意,格式/类型组合是您提供给 three.js 的数据的格式和类型,不是将存储数据的纹理格式。

指定您设置的内部格式texture.internalFormat。要指定formattype,您可以设置texture.formattexture.type

只有某些格式/类型的组合可以用作给定内部格式的输入。有关列表,请参见上面的链接。

如果你想要 1 个通道 unsigned int 纹理,你需要设置

texture.internalFormat = 'R32UI';
texture.format = THREE.RedIntegerFormat;
texture.type = THREE.UnsignedIntType;

您还需要确保将texture.minFiltertexture.magFilter 设置为THREE.NearestFilter,并且您需要确保您的着色器使用uniform usampler3D someSamplerName;

<canvas id="c"></canvas>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';

function main() {
  const canvas = document.querySelector('#c');
  const context = canvas.getContext('webgl2');
  const renderer = new THREE.WebGLRenderer({canvas, context});

  const fov = 75;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 5;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.z = 2;

  const scene = new THREE.Scene();

  const data = new Uint32Array([1234]);
  const texture = new THREE.DataTexture3D(data, 1, 1, 1);
  texture.internalFormat = 'R32UI';
  texture.format = THREE.RedIntegerFormat;
  texture.type = THREE.UnsignedIntType;

  const boxWidth = 1;
  const boxHeight = 1;
  const boxDepth = 1;
  const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);

  const shader = {
    uniforms: {
      threeD: { value: texture },
    },
    vertexShader: `#version 300 es
      void main() {
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
      }
    `,
    fragmentShader: `#version 300 es
      #include <common>

      uniform highp usampler3D threeD;
      out vec4 c;
      
      void main() {
        uvec4 color = texture(threeD, vec3(0));
        // this also works
        // uvec4 color = texelFetch(threeD, ivec3(0), 0);
        c = vec4(float(color.r) / 2468.0, 0, 0, 1);
      }
    `,
  };


  const material = new THREE.ShaderMaterial(shader);

  const cube = new THREE.Mesh(geometry, material);
  scene.add(cube);

  renderer.render(scene, camera);
}

main();
</script>

使用 RGIntegerFormat 和 UnsignedByteType 您需要使用内部格式 RG8UI 并将您的数据作为 Uint8Array 传递

<canvas id="c"></canvas>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';

function main() {
  const canvas = document.querySelector('#c');
  const context = canvas.getContext('webgl2');
  const renderer = new THREE.WebGLRenderer({canvas, context});

  const fov = 75;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 5;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.z = 2;

  const scene = new THREE.Scene();

// THREE.RGIntegerFormat as format and THREE.UnsignedByteType

  const data = new Uint8Array([11, 22]);
  const texture = new THREE.DataTexture3D(data, 1, 1, 1);
  texture.internalFormat = 'RG8UI';
  texture.format = THREE.RGIntegerFormat;
  texture.type = THREE.UnsignedByteType;
window.t = THREE;
  const boxWidth = 1;
  const boxHeight = 1;
  const boxDepth = 1;
  const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);

  const shader = {
    uniforms: {
      threeD: { value: texture },
    },
    vertexShader: `#version 300 es
      void main() {
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
      }
    `,
    fragmentShader: `#version 300 es
      #include <common>

      uniform highp usampler3D threeD;
      out vec4 c;
      
      void main() {
        uvec4 color = texture(threeD, vec3(0));
        // this also works
        // uvec4 color = texelFetch(threeD, ivec3(0), 0);
        c = vec4(vec2(color.rg) / vec2(22), 0, 1);
      }
    `,
  };


  const material = new THREE.ShaderMaterial(shader);

  const cube = new THREE.Mesh(geometry, material);
  scene.add(cube);

  renderer.render(scene, camera);
}

main();
</script>

【讨论】:

  • 非常感谢@gman,但我仍然有困难,当我将 THREE.RGIntegerFormat 作为格式和 THREE.UnsignedByteType 时,这个组合在 webgl2 的表中,每像素的源字节数为 2 . 输入数据是 uint16 但是这给出了无效内部格式的错误。
  • 源的类型始终与类型匹配。因此,如果您键入的是 UnsignedByteType,那么您将使用 Uint8Array。此外,RGIntegerFormat 仅适用于 RG 整数内部格式,例如 RG8I、RG8UI、RG16I、RG16UI、RG32I、RG32UI。
  • 在您提到的页面上,有一个表格,用于列出内部格式、类型、格式和每个像素的源字节。每个像素的源字节与输入数据有关吗?当我有 16 位数据时,我应该在每个像素的源字节中查找值 2 吗?输入数据类型是 uint16,所以即使是 16 位数据,我也输入了 UnsignedByteType。你能给我推荐 Uint16 数据吗?我想构建一个函数,如果需要,根据数据类型返回类型和格式或内部类型(数据类型在 uint16 等变量中可用)。
  • srcData = Uint16Array,内部格式 = RG16UI,格式 = RGIntegerFormat,类型 = UnsignedShortType。然后每个像素有两个 16 位值。
  • 对于 uint16,尽管我设置了 unpackalignemnt =1,但这个值给出了 arraybufferview 的错误,对于请求来说不够大。 unit16 适用于 type=UNSIGNED_SHORT_4_4_4_4 和 format = RGBA ?
猜你喜欢
  • 2023-03-20
  • 2015-10-24
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 2015-01-02
相关资源
最近更新 更多